-
Notifications
You must be signed in to change notification settings - Fork 1
/
main.go
53 lines (49 loc) · 1.33 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
package main
import (
"github.com/jamillosantos/websocket"
"github.com/valyala/fasthttp"
"log"
"time"
)
func main() {
log.SetFlags(log.Lshortfile)
server := &fasthttp.Server{}
manager := websocket.NewListeableManager()
manager.ReadTimeout = time.Second * 10
manager.OnMessageError = func(conn websocket.Connection, err error) {
log.Println(err)
}
manager.OnConnect = func(conn websocket.Connection) error {
log.Println("Incoming client at", conn.Conn().RemoteAddr())
return nil
}
manager.OnMessage = func(conn websocket.Connection, opcode websocket.MessageType, payload []byte) error {
log.Println("OnMessage", opcode, len(payload))
conn.WriteMessage(opcode, payload)
return nil
}
manager.OnClose = func(conn websocket.Connection) error {
log.Println("see ya", conn.Conn().RemoteAddr())
return nil
}
upgrader := websocket.NewUpgrader(manager)
upgrader.Error = func(ctx *fasthttp.RequestCtx, reason error) {
log.Println(reason)
}
server.Handler = func(ctx *fasthttp.RequestCtx) {
log.Println("Connection received.")
upgrader.Upgrade(ctx)
/*
switch string(ctx.URI().Path()) {
case "/ws":
upgrader.Upgrade(ctx)
case "/":
fmt.Fprint(ctx, "This is the root of the server")
default:
fmt.Fprint(ctx, "404 Not Found")
ctx.SetStatusCode(fasthttp.StatusNotFound)
}
*/
}
server.ListenAndServe(":9001")
}