socket.io library. current version.
This commit is contained in:
72
examples/client.go
Normal file
72
examples/client.go
Normal file
@ -0,0 +1,72 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"funstream/libs/socket.io"
|
||||
"funstream/libs/socket.io/transport"
|
||||
"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("localhost:3811", transport.GetDefaultWebsocketTransport())
|
||||
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)
|
||||
}
|
||||
|
||||
err = c.On(gosocketio.OnDisconnection, func(h *gosocketio.Channel, args interface{}) {
|
||||
log.Fatal("Disconnected")
|
||||
})
|
||||
if err != nil {
|
||||
log.Fatal(err)
|
||||
}
|
||||
|
||||
err = c.On(gosocketio.OnConnection, func(h *gosocketio.Channel, args interface{}) {
|
||||
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")
|
||||
}
|
46
examples/server.go
Normal file
46
examples/server.go
Normal file
@ -0,0 +1,46 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"funstream/libs/socket.io"
|
||||
"funstream/libs/socket.io/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, args interface{}) {
|
||||
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, args interface{}) {
|
||||
log.Println("Disconnected")
|
||||
})
|
||||
|
||||
server.On("/join", func(c *gosocketio.Channel, channel Channel) string {
|
||||
time.Sleep(2 * time.Second)
|
||||
return "joined to " + channel.Channel
|
||||
})
|
||||
|
||||
serveMux := http.NewServeMux()
|
||||
serveMux.Handle("/socket.io/", server)
|
||||
|
||||
log.Println("Starting server...")
|
||||
log.Panic(http.ListenAndServe(":3811", serveMux))
|
||||
}
|
Reference in New Issue
Block a user