-
Notifications
You must be signed in to change notification settings - Fork 0
/
controller.go
54 lines (51 loc) · 1.22 KB
/
controller.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
package main
import(
"log"
"net/http"
"encoding/json"
"strconv"
)
func mainpage(w http.ResponseWriter,r *http.Request){
// http.ServeFile(w,r,"test.html")
http.ServeFile(w,r,"html/chat.html")
}
//reference: https://pkg.go.dev/github.com/gorilla/websocket
func sock(w http.ResponseWriter, r * http.Request){
conn,err:=upgrader.Upgrade(w,r,nil)
session,_:=store.Get(r,"cookie-name")
if err != nil {
log.Println(err)
return
}
for {
messageType, value, err := conn.ReadMessage()
if err != nil {
log.Println(err)
// return
}
session.Values["userid"]=cut(conn.RemoteAddr().String())
session.Save(r,w)
log.Println(session.Values["userid"])
//todo json api
location:=cut(conn.RemoteAddr().String())
_,ok:=counter[location]
if(!ok){
count++
counter[location]="1";
}
location=strconv.Itoa(count)
log.Println(location)
api=Profile{
Uid:location,
Messages:string(value),
}
/// i need to add cookie of only userid
if err := conn.WriteMessage(messageType, value); err != nil {
log.Println(err)
return
}
}
}
func jsonthrow(w http.ResponseWriter, r *http.Request){
json.NewEncoder(w).Encode(api)
}