/
chatserver.go
98 lines (79 loc) · 1.78 KB
/
chatserver.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
package main
import (
"golang.org/x/net/websocket"
"time"
"math/rand"
"net/http"
"strconv"
"encoding/json"
"os"
"mygo01/cfg"
"fmt"
)
var Config cfg.Configuration
func load(configfile string) cfg.Configuration {
config := cfg.Configuration{}
file, _ := os.Open(configfile)
decoder := json.NewDecoder(file)
err := decoder.Decode(&config)
if err != nil {
panic(err.Error())
}
return config
}
func wsHandler(ws *websocket.Conn) {
// //p := unsafe.Pointer(&ws)
// //index := ((int)(uintptr(p))) % n
// index := rand.Intn(10)
// lock := locks[index]
// lock.Lock()
// wsList[index].PushBack(ws)
// lock.Unlock()
//
// for {
// var reply string
// if err := websocket.Message.Receive(ws, &reply); err != nil {
// fmt.Println("Can't receive because of " + err.Error())
// break
// }
// fmt.Println(reply)
// }
for{
msg := make([]byte, 12)
n, err := ws.Read(msg)
if err != nil {
fmt.Println("Can't read because of " + err.Error())
}
fmt.Printf("Receive: %s\n", msg[:n])
send_msg := "[" + string(msg[:n]) + "]"
_, err = ws.Write([]byte(send_msg))
if err != nil {
fmt.Println("Can't write because of " + err.Error())
}
fmt.Printf("Send: %s\n", send_msg)
}
//
// lock.Lock()
// for e := wsList[index].Front(); e != nil; e = e.Next() {
// if e.Value.(*websocket.Conn) == ws {
// wsList[index].Remove(e)
// break
// }
// }
// lock.Unlock()
fmt.Println("123")
}
func main() {
seed := time.Now().UTC().UnixNano()
rand.Seed(seed)
Config = load("config.json")
fmt.Print("123")
http.HandleFunc("/", func(w http.ResponseWriter, req *http.Request) {
s := websocket.Server{Handler: websocket.Handler(wsHandler)}
s.ServeHTTP(w, req)
})
err := http.ListenAndServe(":"+strconv.Itoa(Config.Port), nil)
if err != nil {
panic("Error: " + err.Error())
}
}