forked from hassansin/go-websocket-echo-server
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.go
More file actions
53 lines (45 loc) · 767 Bytes
/
Copy pathmain.go
File metadata and controls
53 lines (45 loc) · 767 Bytes
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
package main
import (
"log"
"net/http"
)
func WsHandle(w http.ResponseWriter, r *http.Request) {
ws, err := New(w, r)
if err != nil {
log.Println(err)
return
}
err = ws.Handshake()
if err != nil {
log.Println(err)
return
}
defer ws.Close()
for {
frame, err := ws.Recv()
if err != nil {
log.Println("Error Decoding", err)
return
}
switch frame.Opcode {
case 8: // Close
return
case 9: // Ping
frame.Opcode = 10
fallthrough
case 0: // Continuation
fallthrough
case 1: // Text
fallthrough
case 2: // Binary
if err = ws.Send(frame); err != nil {
log.Println("Error sending", err)
return
}
}
}
}
func main() {
http.HandleFunc("/", WsHandle)
log.Fatal(http.ListenAndServe(":9001", nil))
}