-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathmain.go
392 lines (319 loc) · 8.65 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
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
package main
import (
"log"
"net/http"
"github.com/graarh/golang-socketio"
"github.com/graarh/golang-socketio/transport"
)
const (
PORT = ":5555"
ROOM_WIRELESS = "wireless"
//EVENT_CONNECTION = "connection"
EVENT_RECEIVER_ON = "event:receiver:on"
EVENT_NEW_RECEIVER = "event:new:receiver"
//EVENT_RECEIVER_ON_OK = "event:receiver:on:ok"
//EVENT_RECEIVER_OFF = "event:receiver:off"
EVENT_SENDER_ON = "event:sender:on"
//EVENT_SENDER_UPDATE = "event:sender:update"
EVENT_PAY = "event:pay"
EVENT_PAYMENT_SEND = "event:payment:send"
CODE_VERSION = "0.0.1"
)
type Receiver struct {
Id string `json:"user_id"`
CurrencyId int `json:"currency_id"`
Amount int64 `json:"amount"`
UserCode string `json:"user_code"`
Socket *gosocketio.Channel
}
type Sender struct {
Id string `json:"user_id"`
UserCode string `json:"user_code"`
Socket *gosocketio.Channel
}
func main() {
initGraarhSockets()
//initGoogleeSockets()
}
func initGraarhSockets() {
server := gosocketio.NewServer(transport.GetDefaultWebsocketTransport())
type User struct {
Socket *gosocketio.Channel
Id string
}
receivers := make(map[string]Receiver)
senders := []Sender{}
//user := User{Id:"Vadim Test"}
//handle connected
server.On(gosocketio.OnConnection, func(c *gosocketio.Channel) {
log.Println("New client connected:", c.Id())
log.Printf("INITIAL ARRAY\n SENDERS: %v \n RECEIVERS:%v", len(senders), len(receivers))
//join them to room
//user.Socket = c
c.Join(ROOM_WIRELESS)
//c.BroadcastTo(ROOM_WIRELESS, EVENT_NEW_RECEIVER, "This is message from the rooom")
})
server.On(gosocketio.OnDisconnection, func(c *gosocketio.Channel) {
log.Printf("Client Disconnected", c.Id())
log.Printf("INITIAL ARRAY\n SENDERS: %v \n RECEIVERS:%v", len(senders), len(receivers))
for _, receiver:= range receivers{
if receiver.Socket.Id() == c.Id(){
delete(receivers, receiver.UserCode)
continue
}
}
for i, sender:=range senders{
if sender.Socket.Id() == c.Id(){
senders = append(senders[:i], senders[i+1:]...)
continue
}
}
log.Printf("FINAL ARRAY\n SENDERS: %v \n RECEIVERS:%v", len(senders), len(receivers))
})
type ReceiverInData struct {
Id string `json:"user_id"`
CurrencyId int `json:"currency_id"`
Amount int64 `json:"amount"`
UserCode string `json:"user_code"`
}
server.On(EVENT_RECEIVER_ON, func(c *gosocketio.Channel, data ReceiverInData) string {
log.Printf("Got messeage Receiver On:", data)
log.Printf("DAta:", data)
//so.Emit(EVENT_RECEIVER_ON_OK, "nice job maaan ;)")
c.Join(ROOM_WIRELESS)
receiver:= Receiver{
Socket:c,
CurrencyId:data.CurrencyId,
Amount:data.Amount,
Id:data.Id,
UserCode:data.UserCode,
}
_, ok := receivers[receiver.UserCode]
if !ok{
receivers[receiver.UserCode]=receiver
}
//Try to find Sender
for _,sender := range senders{
if sender.UserCode == receiver.UserCode{
sender.Socket.Emit(EVENT_NEW_RECEIVER, receiver)
}
}
return "ok"
})
type SenderInData struct {
Code string `json:"user_code"`
UserId string `json:"user_id"`
}
//handle custom event
server.On(EVENT_SENDER_ON, func(c *gosocketio.Channel, data SenderInData) string {
log.Printf("Sender become on:", c.Id())
sender:= Sender{UserCode:data.Code, Id:data.UserId, Socket:c }
log.Printf("God data from sender:", sender)
var senderExist bool = false
for _, cachedSender:= range senders{
if cachedSender.Id == sender.Id{
senderExist = true
}
}
if !senderExist{
senders = append(senders, sender)
}
// try to find Receiver by the code
receiver, ok := receivers[sender.UserCode]
if ok{
sender.Socket.Emit(EVENT_NEW_RECEIVER, receiver)
} else{
var senderExist bool = false
for _, cachedSender:= range senders{
if cachedSender.Id == sender.Id{
senderExist = true
}
}
if !senderExist{
senders = append(senders, sender)
}
}
return "ok"
})
type PaymentData struct {
FromId string `json:"from_id"`
ToId string `json:"to_id"`
CurrencyId int `json:"currency_id"`
Amount int64 `json:"amount"`
}
server.On(EVENT_PAY, func(c *gosocketio.Channel, data PaymentData ) string {
receiver, ok := receivers[data.ToId]
if (ok){
//receiver is exist and we should notify him about the payment
receiver.Socket.Emit(EVENT_PAYMENT_SEND, data)
} else {
return "fail"
}
return "ok"
})
//setup http server
serveMux := http.NewServeMux()
serveMux.Handle("/socket.io/", server)
log.Println("Serving at localhost" + PORT)
log.Printf("Code version: %v", CODE_VERSION)
log.Panic(http.ListenAndServe(PORT, serveMux))
}
//func initGoogleeSockets(){
//
// server, err := socketio.NewServer(nil)
// if err != nil {
// log.Fatal(err)
// }
//
// receivers := make(map[int]Receiver)
// senders := []Sender{}
//
//
// server.On(EVENT_CONNECTION, func(so socketio.Socket) {
// log.Println("on connection")
// log.Printf("So id:", so.Id())
// log.Printf("So request", so.Request().GetBody)
//
// so.Join(ROOM_WIRELESS)
// so.BroadcastTo(ROOM_WIRELESS, EVENT_NEW_RECEIVER, "HEYYYS can brodcast")
// //log.Printf("Some data:", so.Id(), so.Request(), so)
//
// so.On(EVENT_RECEIVER_ON, func(data interface{} ) string {
// log.Printf("So id Event RECEIVER ON:", so.Id())
// log.Printf("DAta:", data)
// //so.Emit(EVENT_RECEIVER_ON_OK, "nice job maaan ;)")
// so.Join(so.Id())
//
// parsed := data.(map[string]interface{})
// user_id:= parsed["user_id"].(string)
// raw_code:= parsed["user_code"].(float64)
// user_code := int(raw_code)
// raw_currency_id := parsed["currency_id"].(float64)
// currency_id := int(raw_currency_id)
// raw_amount := parsed["amount"].(float64)
// amount := int64(raw_amount)
//
// receiver := Receiver{
// Id:user_id,
// UserCode:user_code,
// CurrencyId:currency_id,
// Amount:amount,
// }
//
//
// _, ok := receivers[receiver.UserCode]
// if !ok{
// receivers[receiver.UserCode]=receiver
// }
//
// //Try to find Sender
// for _,sender := range senders{
// if sender.UserCode == receiver.UserCode{
// so.BroadcastTo(ROOM_WIRELESS, EVENT_NEW_RECEIVER, "HEY")
// }
// }
//
// return "welcome:to:receiver:side"
// })
//
// so.On(EVENT_RECEIVER_OFF, func(data interface{} ) string {
// log.Printf("So id Event RECEIVER OFF:", so.Id())
// log.Printf("DAta:", data)
// //so.Emit(EVENT_RECEIVER_ON_OK, "nice job maaan ;)")
// //TODO add receiver off logic here
// return "goodbye receiver"
// })
//
// so.On(EVENT_SENDER_ON, func(data interface{} ) string {
// log.Printf("Sender become on:", so.Id())
//
// //map[string]interface {}=map[user_id:Vadddim user_code:3252])
// parsed := data.(map[string]interface{})
// user_id:= parsed["user_id"].(string)
// raw_code:= parsed["user_code"].(float64)
// user_code := int(raw_code)
//
//
//
//
// log.Printf("God data from sender:", user_id, user_code)
//
//
//
// sender:= Sender{UserCode:user_code, Id:user_id }
//
// so.BroadcastTo(ROOM_WIRELESS, EVENT_NEW_RECEIVER, "HEyyyy")
//
// var senderExist bool = false
//
// for _, cachedSender:= range senders{
// if cachedSender.Id == sender.Id{
// senderExist = true
// }
// }
//
//
// if !senderExist{
// senders = append(senders, sender)
// }
//
// // try to find Receiver by the code
// receiver, ok := receivers[sender.UserCode]
// if ok{
// receiver.Id = "new ID"
// so.BroadcastTo(ROOM_WIRELESS, EVENT_NEW_RECEIVER, "Heeey")
// } else{
// var senderExist bool = false
//
// for _, cachedSender:= range senders{
// if cachedSender.Id == sender.Id{
// senderExist = true
// }
// }
//
//
// if !senderExist{
// senders = append(senders, sender)
// }
//
//
// }
//
// return "hello sender"
// })
//
//
// //so.On(EVENT_SENDER_OFF, func(data interface{} ) string {
// // log.Printf("Sender become off:", so.Id())
// // //TODO add sender disconnect logic here
// // return "goodbye sender"
// //})
// //
// //so.On(EVENT_SENDER_UPDATE, func(data interface{} ) string {
// // log.Printf("Sender update called:", so.Id(), data)
// // //TODO add sender update logic here
// // return "goodbye sender"
// //})
//
//
// so.On("disconnection", func() {
// log.Printf("So id:", so.Id())
// log.Println("on disconnect")
// //TODO add disconnect socket user logic
// })
// })
// server.On("error", func(so socketio.Socket, err error) {
// log.Printf("Some data:", so.Id(), so.Request(), so)
// log.Println("error:", err)
// })
//
//
//
//
//
//
// http.Handle("/socket.io/", server)
// //http.Handle("/", http.FileServer(http.Dir("./asset")))
// log.Println("Serving at localhost" + PORT)
// log.Fatal(http.ListenAndServe(PORT, nil))
//}