-
Notifications
You must be signed in to change notification settings - Fork 258
Expand file tree
/
Copy pathserver.go
More file actions
47 lines (37 loc) · 1.1 KB
/
Copy pathserver.go
File metadata and controls
47 lines (37 loc) · 1.1 KB
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
package main
import (
"github.com/gsocket-io/golang-socketio"
"github.com/gsocket-io/golang-socketio/transport"
"log"
"net/http"
"time"
)
type Channel struct {
Channel string `json:"channel"`
}
type Message struct {
Id int `json:"id"`
Channel string `json:"channel"`
Text string `json:"text"`
}
func main() {
server := gosocketio.NewServer(transport.GetDefaultWebsocketTransport())
server.On(gosocketio.OnConnection, func(c *gosocketio.Channel) {
log.Println("Connected")
c.Emit("/message", Message{10, "main", "using emit"})
c.Join("test")
c.BroadcastTo("test", "/message", Message{10, "main", "using broadcast"})
})
server.On(gosocketio.OnDisconnection, func(c *gosocketio.Channel) {
log.Println("Disconnected")
})
server.On("/join", func(c *gosocketio.Channel, channel Channel) string {
time.Sleep(2 * time.Second)
log.Println("Client joined to ", channel.Channel)
return "joined to " + channel.Channel
})
serveMux := http.NewServeMux()
serveMux.Handle("/socket.io/", server)
log.Println("Starting server...")
log.Panic(http.ListenAndServe(":3811", serveMux))
}