golang-socketio/examples/client.go

75 lines
1.3 KiB
Go
Raw Normal View History

2016-05-18 22:11:10 +02:00
package main
import (
"forge.cadoles.com/Pyxis/golang-socketio"
"forge.cadoles.com/Pyxis/golang-socketio/transport"
2016-05-18 22:11:10 +02:00
"log"
"runtime"
"time"
)
type Channel struct {
Channel string `json:"channel"`
}
type Message struct {
Id int `json:"id"`
Channel string `json:"channel"`
Text string `json:"text"`
}
func sendJoin(c *gosocketio.Client) {
log.Println("Acking /join")
result, err := c.Ack("/join", Channel{"main"}, time.Second*5)
if err != nil {
log.Fatal(err)
} else {
log.Println("Ack result to /join: ", result)
}
}
func main() {
runtime.GOMAXPROCS(runtime.NumCPU())
c, err := gosocketio.Dial(
gosocketio.GetUrl("localhost", 3811, false),
transport.GetDefaultWebsocketTransport())
2016-05-18 22:11:10 +02:00
if err != nil {
log.Fatal(err)
}
err = c.On("/message", func(h *gosocketio.Channel, args Message) {
log.Println("--- Got chat message: ", args)
})
if err != nil {
log.Fatal(err)
}
2016-05-22 20:04:25 +02:00
err = c.On(gosocketio.OnDisconnection, func(h *gosocketio.Channel) {
2016-05-18 22:11:10 +02:00
log.Fatal("Disconnected")
})
if err != nil {
log.Fatal(err)
}
2016-05-22 20:04:25 +02:00
err = c.On(gosocketio.OnConnection, func(h *gosocketio.Channel) {
2016-05-18 22:11:10 +02:00
log.Println("Connected")
})
if err != nil {
log.Fatal(err)
}
time.Sleep(1 * time.Second)
go sendJoin(c)
go sendJoin(c)
go sendJoin(c)
go sendJoin(c)
go sendJoin(c)
time.Sleep(60 * time.Second)
c.Close()
log.Println(" [x] Complete")
}