-
Notifications
You must be signed in to change notification settings - Fork 0
/
socket.go
125 lines (100 loc) · 3.52 KB
/
socket.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
package main
import (
"github.com/gorilla/websocket"
"github.com/jeffail/gabs"
"net/http"
//"sync"
"fmt"
"log"
//types "./Package/Types/"
)
func SendMessageToClient(sockId uint64, msg string) {
ActiveClients[sockId].websocket.WriteMessage(websocket.TextMessage, []byte(msg))
}
func SendMessageToAllClients(msg string) {
for _, Client := range ActiveClients {
ActiveClients[Client.Socket_Id].websocket.WriteMessage(websocket.TextMessage, []byte(msg))
}
}
func (s *NumberConnectionType) GetNew() uint64{
s.Lock()
s.counters+=1
defer s.Unlock()
return s.counters
}
func ClientSockServer(w http.ResponseWriter, r *http.Request) {
//vars := mux.Vars(r) //vars["key"]
var SocId uint64=CounterConnections.GetNew()
conn, err := websocket.Upgrade(w, r, nil, 1024, 1024)
if _, ok := err.(websocket.HandshakeError); ok {
http.Error(w, "Not a websocket handshake", 400)
return
}
var sess Session
sess=getSession(r)
Id := int(sess.UserId)
FIO := UsersList[int(sess.UserId)].FIO
log.Println("Клиент подкл.Сокет-",SocId," ", Id," ",FIO)
defer conn.Close();
jsonObj:= gabs.New()
jsonObj.Set("DDOS_Start", "Command")
jsonObj.Set("http://localhost:5001/", "Data", "Site")
jsonObj.Set(25, "Data", "Routine_numb")
SendMessageToAllClients(jsonObj.String())
for {
_, msg, err := conn.ReadMessage()
msg1:=fmt.Sprintf("%s",msg)
mesg:=[]byte(msg1)
//msg_ := []byte(bytes.Index(byteArray, []byte{0}))
if err != nil {
log.Println("Клиент откл .Сокет-",SocId," ", Id," ",FIO)
delete(ActiveClients,SocId)
/*
jsonObj_:= gabs.New()
jsonObj_.Array("Clients")
for _, Client := range ActiveClients {
jsonObj_.ArrayAppend(Client, "Clients")
}
jsonObj_.Set("Clients_List", "Command")
jsonObj:= gabs.New()
jsonObj.Set("Clients_List", "Command")
*/
//SendMessageToAllAdmins(jsonObj_.String())
return
}
var Command string
var ok bool
jsonParsed, err_ := gabs.ParseJSON(mesg)
if err_ != nil {
log.Println("Данные от клиента пришли не в json формате")
return
}
Command, ok= jsonParsed.Path("Command").Data().(string)
if ok == false {
log.Println("Данные от клиента пришли без команды")
return
}
switch Command {
default:
log.Println("Функция,которую отослал клиент, не распознана- ")
}
}
}
//--------Send Message
/*
func SendMessageToClient(sockId uint64, msg string) {
ActiveClients[sockId].websocket.WriteMessage(websocket.TextMessage, []byte(msg))
}
func SendMessageToAllClients(msg string) {
for _, Client := range ActiveClients {
ActiveClients[Client.Socket_Id].websocket.WriteMessage(websocket.TextMessage, []byte(msg))
}
}
func SendMessageToAdmin(sockId uint64, msg string) {
// ActiveAdmins[sockId].websocket.WriteMessage(websocket.TextMessage, []byte(msg))
}
func SendMessageToAllAdmins(msg string) {
for _, Admin := range ActiveAdmins {
ActiveAdmins[Admin.Socket_Id].websocket.WriteMessage(websocket.TextMessage, []byte(msg))
}
}*/