2016-05-18 22:11:10 +02:00
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
|
|
|
"log"
|
|
|
|
"net/http"
|
|
|
|
"time"
|
2024-08-05 17:53:36 +02:00
|
|
|
|
|
|
|
gosocketio "forge.cadoles.com/Pyxis/golang-socketio"
|
|
|
|
"forge.cadoles.com/Pyxis/golang-socketio/transport"
|
2016-05-18 22:11:10 +02:00
|
|
|
)
|
|
|
|
|
|
|
|
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())
|
|
|
|
|
2016-05-22 20:04:25 +02:00
|
|
|
server.On(gosocketio.OnConnection, func(c *gosocketio.Channel) {
|
2016-05-18 22:11:10 +02:00
|
|
|
log.Println("Connected")
|
|
|
|
|
|
|
|
c.Emit("/message", Message{10, "main", "using emit"})
|
|
|
|
|
|
|
|
c.Join("test")
|
|
|
|
c.BroadcastTo("test", "/message", Message{10, "main", "using broadcast"})
|
|
|
|
})
|
2016-05-22 20:04:25 +02:00
|
|
|
server.On(gosocketio.OnDisconnection, func(c *gosocketio.Channel) {
|
2016-05-18 22:11:10 +02:00
|
|
|
log.Println("Disconnected")
|
|
|
|
})
|
|
|
|
|
|
|
|
server.On("/join", func(c *gosocketio.Channel, channel Channel) string {
|
|
|
|
time.Sleep(2 * time.Second)
|
2016-05-22 20:04:25 +02:00
|
|
|
log.Println("Client joined to ", channel.Channel)
|
2016-05-18 22:11:10 +02:00
|
|
|
return "joined to " + channel.Channel
|
|
|
|
})
|
|
|
|
|
|
|
|
serveMux := http.NewServeMux()
|
|
|
|
serveMux.Handle("/socket.io/", server)
|
|
|
|
|
|
|
|
log.Println("Starting server...")
|
|
|
|
log.Panic(http.ListenAndServe(":3811", serveMux))
|
|
|
|
}
|