commit b38a7bac202ad0e211dc17b304fe7fc11aa931c8
parent c758e56937cc3273b482d304a0e07880924ab3cd
Author: francoispqt <francois@parquet.ninja>
Date: Thu, 3 May 2018 00:25:04 +0800
update websocket example
Diffstat:
2 files changed, 12 insertions(+), 3 deletions(-)
diff --git a/examples/websocket/client/client.go b/examples/websocket/client/client.go
@@ -22,5 +22,6 @@ func (c *client) Dial(url, origin string) error {
return err
}
c.Conn = conn
+ c.Init(10)
return nil
}
diff --git a/examples/websocket/main.go b/examples/websocket/main.go
@@ -1,8 +1,16 @@
// package main simulates a conversation between
// a given set of websocket clients and a server.
//
-// It uses gojay's streaming feature to abstract JSON communication
-// and only having to handle go values.
+// It spins up a web socket server.
+// On a client's connection it creates a SenderReceiver which handles JSON Stream
+// encoding and decoding using gojay's streaming API to abstract JSON communication
+// between server and client, only having to handle go values.
+//
+// To simulate a conversation:
+// - the server sends a welcome message to the client
+// - when the client receives the message, it sends a message back to the server
+// - when the server receives the ack message, it will send a message randomly to a client
+// - when the client receives the message, it sends a message back to the server... and so on.
package main
import (
@@ -44,7 +52,7 @@ func createClient(url, origin string, i int) {
str := strconv.Itoa(i)
// Init client's sender and receiver
// Set the OnMessage handler
- c.Init(10).OnMessage(func(m *comm.Message) {
+ c.OnMessage(func(m *comm.Message) {
log.Print("client "+str+" received from "+m.UserName+" message: ", m)
c.SendMessage(&comm.Message{
UserName: str,