2016-05-18 22:11:10 +02:00
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
2018-09-18 17:59:07 +02:00
|
|
|
"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())
|
|
|
|
|
2016-05-20 14:26:18 +02:00
|
|
|
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")
|
|
|
|
}
|