-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
61 lines (50 loc) · 1.25 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
54
55
56
57
58
59
60
61
package main
import (
"log"
"net/http"
socketio "github.com/googollee/go-socket.io"
"github.com/googollee/go-socket.io/engineio"
"github.com/googollee/go-socket.io/engineio/transport"
"github.com/googollee/go-socket.io/engineio/transport/websocket"
)
func main() {
server := socketio.NewServer(
&engineio.Options{
Transports: []transport.Transport{
&websocket.Transport{
CheckOrigin: func(r *http.Request) bool {
return true
},
},
},
},
)
server.OnConnect("/", func(s socketio.Conn) error {
s.SetContext("")
log.Println("Connected:", s.ID())
return nil
})
server.OnEvent("/", "start", func(s socketio.Conn, msg string) {
s.SetContext(msg)
err := startCosmos(func(msg string) {
log.Println(msg)
s.Emit("new_message", msg)
})
if err != nil {
s.Close()
log.Fatal(err)
}
})
server.OnError("/", func(s socketio.Conn, e error) {
log.Println("Error:", e)
})
server.OnDisconnect("/", func(s socketio.Conn, reason string) {
log.Println("Disconnected:", reason)
})
go server.Serve()
defer server.Close()
http.Handle("/socket.io/", server)
http.Handle("/", http.FileServer(http.Dir("./static")))
log.Println("Listening at localhost:8000...")
log.Fatal(http.ListenAndServe(":8000", nil))
}