mirror of
https://github.com/kataras/iris.git
synced 2025-01-24 11:11:03 +01:00
35389c6ef8
Former-commit-id: c34b916e186286cd0de6d694d8bdb4f9390121a2
85 lines
1.9 KiB
Go
85 lines
1.9 KiB
Go
package main
|
|
|
|
import (
|
|
"bufio"
|
|
"bytes"
|
|
"context"
|
|
"fmt"
|
|
"log"
|
|
"os"
|
|
"time"
|
|
|
|
"github.com/kataras/iris/websocket"
|
|
|
|
"github.com/kataras/neffos"
|
|
)
|
|
|
|
const (
|
|
endpoint = "ws://localhost:8080/echo"
|
|
namespace = "default"
|
|
dialAndConnectTimeout = 5 * time.Second
|
|
)
|
|
|
|
// this can be shared with the server.go's.
|
|
// `NSConn.Conn` has the `IsClient() bool` method which can be used to
|
|
// check if that's is a client or a server-side callback.
|
|
var clientEvents = neffos.Namespaces{
|
|
namespace: neffos.Events{
|
|
neffos.OnNamespaceConnected: func(c *neffos.NSConn, msg neffos.Message) error {
|
|
log.Printf("connected to namespace: %s", msg.Namespace)
|
|
return nil
|
|
},
|
|
neffos.OnNamespaceDisconnect: func(c *neffos.NSConn, msg neffos.Message) error {
|
|
log.Printf("disconnected from namespace: %s", msg.Namespace)
|
|
return nil
|
|
},
|
|
"chat": func(c *neffos.NSConn, msg neffos.Message) error {
|
|
log.Printf("%s", string(msg.Body))
|
|
return nil
|
|
},
|
|
},
|
|
}
|
|
|
|
func main() {
|
|
ctx, cancel := context.WithDeadline(context.Background(), time.Now().Add(dialAndConnectTimeout))
|
|
defer cancel()
|
|
|
|
client, err := neffos.Dial(ctx, websocket.DefaultGorillaDialer, endpoint, clientEvents)
|
|
if err != nil {
|
|
panic(err)
|
|
}
|
|
defer client.Close()
|
|
|
|
c, err := client.Connect(ctx, namespace)
|
|
if err != nil {
|
|
panic(err)
|
|
}
|
|
|
|
c.Emit("chat", []byte("Hello from Go client side!"))
|
|
|
|
fmt.Fprint(os.Stdout, ">> ")
|
|
scanner := bufio.NewScanner(os.Stdin)
|
|
for {
|
|
if !scanner.Scan() {
|
|
log.Printf("ERROR: %v", scanner.Err())
|
|
return
|
|
}
|
|
|
|
text := scanner.Bytes()
|
|
|
|
if bytes.Equal(text, []byte("exit")) {
|
|
if err := c.Disconnect(nil); err != nil {
|
|
log.Printf("reply from server: %v", err)
|
|
}
|
|
break
|
|
}
|
|
|
|
ok := c.Emit("chat", text)
|
|
if !ok {
|
|
break
|
|
}
|
|
|
|
fmt.Fprint(os.Stdout, ">> ")
|
|
}
|
|
} // try running this program twice or/and run the server's http://localhost:8080 to check the browser client as well.
|