-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
147 lines (126 loc) · 3.2 KB
/
main.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
package main
import (
"encoding/json"
"fmt"
"io/ioutil"
"log"
"math/rand"
"net/http"
"strconv"
"time"
socketio "github.com/googollee/go-socket.io"
)
/**
flutter reacgt-native
荣耀v10 1ms 15ms
三星S6 1ms 25ms
红米2A 3ms 60ms
*/
// INTERVAL 数据推送时间间隔(毫秒)
const INTERVAL = 10
// NUMBER 每次更新的行情数量
const NUMBER = 10
// PORT 服务端口号
const PORT = ":12345"
var sockets map[string]socketio.Socket
var initData []quotation
var tickData = make([]quotation, NUMBER)
type quotation struct {
ContractNo string `json:"contractNo"`
ContractName string `json:"contractName"`
ChangeRate float64 `json:"changeRate,string"`
ChangeValue float64 `json:"changeValue,string"`
LastPrice float64 `json:"lastPrice,string"`
PreClosingPrice float64 `json:"preClosingPrice,string"`
}
func init() {
list, err := readFile("data.json")
if err != nil {
fmt.Println("init data failed: ", err.Error())
}
initData = list
}
func readFile(filename string) ([]quotation, error) {
var list []quotation
bytes, err := ioutil.ReadFile(filename)
if err != nil {
fmt.Println("ReadFile failed: ", err.Error())
return nil, err
}
if err := json.Unmarshal(bytes, &list); err != nil {
fmt.Println("Unmarshal failed: ", err.Error())
return nil, err
}
return list, nil
}
func shuffle(vals []quotation) []quotation {
r := rand.New(rand.NewSource(time.Now().Unix()))
n := len(vals)
ret := make([]quotation, n)
perm := r.Perm(n)
for i, randIndex := range perm {
ret[i] = vals[randIndex]
}
return ret
}
func main() {
server, err := socketio.NewServer(nil)
if err != nil {
log.Fatal(err)
}
dataTicker := time.NewTicker(time.Millisecond * INTERVAL)
go func() {
for {
select {
case <-dataTicker.C:
rs := shuffle(initData)[:NUMBER]
data := make([]quotation, NUMBER)
for i, item := range rs {
x := rand.Intn(100)
isNegative := x/2 == 1
rate := float64(x) / 1000
if isNegative {
rate = rate * -1
}
item.ChangeRate = rate
item.ChangeValue = item.PreClosingPrice * rate
LastPrice := item.PreClosingPrice * (1 + rate)
item.LastPrice, _ = strconv.ParseFloat(fmt.Sprintf("%.5f", LastPrice), 64)
data[i] = item
}
tickData = data
}
}
}()
senderTicker := time.NewTicker(time.Millisecond * INTERVAL)
go func() {
for {
select {
case <-senderTicker.C:
fmt.Printf("ticked at %v %v %v\n", time.Now(), tickData[0].ContractName, tickData[0].LastPrice)
server.BroadcastTo("quotation", "mdDataEvent", tickData)
}
}
}()
server.On("connection", func(soc socketio.Socket) {
log.Println("on connection", soc.Id())
// sockets[soc.Id()] = soc
soc.On("registerEvent", func(msg string) {
log.Println("on registerEvent", msg)
soc.Join("quotation")
// 单个
soc.Emit("mdListEvent", initData)
})
soc.On("disconnection", func() {
log.Println("on disconnect")
soc.Leave("quotation")
})
})
server.On("error", func(soc socketio.Socket, err error) {
log.Println("error:", err)
})
http.Handle("/socket.io/", server)
http.Handle("/", http.FileServer(http.Dir("./public")))
log.Println("Serving at localhost", PORT)
log.Fatal(http.ListenAndServe(PORT, nil))
}