diff --git a/_examples/basic/main.go b/_examples/basic/main.go index 999f7ec..66b1e32 100644 --- a/_examples/basic/main.go +++ b/_examples/basic/main.go @@ -20,6 +20,10 @@ const ( addr = "localhost:8080" endpoint = "/echo" namespace = "default" + // false if client sends a join request. + serverJoinRoom = false + // if the above is true then this field should be filled, it's the room name that server force-joins a namespace connection. + serverRoomName = "room1" ) // userMessage implements the `MessageBodyUnmarshaler` and `MessageBodyMarshaler`. @@ -42,16 +46,20 @@ var serverAndClientEvents = neffos.Namespaces{ namespace: neffos.Events{ neffos.OnNamespaceConnected: func(c *neffos.NSConn, msg neffos.Message) error { log.Printf("[%s] connected to namespace [%s].", c, msg.Namespace) + + if !c.Conn.IsClient() && serverJoinRoom { + c.JoinRoom(nil, serverRoomName) + } + return nil }, neffos.OnNamespaceDisconnect: func(c *neffos.NSConn, msg neffos.Message) error { log.Printf("[%s] disconnected from namespace [%s].", c, msg.Namespace) return nil }, - neffos.OnRoomJoined: func(c *neffos.NSConn, msg neffos.Message) error { text := fmt.Sprintf("[%s] joined to room [%s].", c, msg.Room) - log.Printf("%s", text) + log.Println(text) // notify others. if !c.Conn.IsClient() { @@ -67,7 +75,7 @@ var serverAndClientEvents = neffos.Namespaces{ }, neffos.OnRoomLeft: func(c *neffos.NSConn, msg neffos.Message) error { text := fmt.Sprintf("[%s] left from room [%s].", c, msg.Room) - log.Printf("%s", text) + log.Println(text) // notify others. if !c.Conn.IsClient() { @@ -81,7 +89,6 @@ var serverAndClientEvents = neffos.Namespaces{ return nil }, - "chat": func(c *neffos.NSConn, msg neffos.Message) error { if !c.Conn.IsClient() { c.Conn.Server().Broadcast(c, msg) @@ -108,6 +115,8 @@ var serverAndClientEvents = neffos.Namespaces{ } func main() { + // neffos.EnableDebug(nil) + args := os.Args[1:] if len(args) == 0 { log.Fatalf("expected program to start with 'server' or 'client' argument") @@ -197,16 +206,26 @@ func startClient() { if err != nil { log.Fatal(err) } + + var room *neffos.Room + askRoom: - fmt.Print("Please specify a room to join, i.e room1: ") - if !scanner.Scan() { - log.Fatal(scanner.Err()) - } - roomToJoin := scanner.Text() + if !serverJoinRoom { + fmt.Print("Please specify a room to join, i.e room1: ") + if !scanner.Scan() { + log.Fatal(scanner.Err()) + } + roomToJoin := scanner.Text() - room, err := c.JoinRoom(nil, roomToJoin) - if err != nil { - log.Fatal(err) + room, err = c.JoinRoom(nil, roomToJoin) + if err != nil { + log.Fatal(err) + } + } else { + room = c.Room(serverRoomName) + if room == nil { + log.Fatalf("room %s is nil", serverRoomName) + } } fmt.Fprint(os.Stdout, ">> ") @@ -225,15 +244,19 @@ askRoom: if text == "leave" { room.Leave(nil) - goto askRoom + if !serverJoinRoom { + goto askRoom + } } // username is the connection's ID == // room.String() returns -> NSConn.String() returns -> Conn.String() returns -> Conn.ID() // which generated by server-side via `Server#IDGenerator`. userMsg := userMessage{From: username, Text: text} - room.Emit("chat", neffos.Marshal(userMsg)) - + ok := room.Emit("chat", neffos.Marshal(userMsg)) + if !ok { + log.Fatal("Emit failed") + } fmt.Fprint(os.Stdout, ">> ") } } diff --git a/go.mod b/go.mod index 53aee6b..333c17f 100644 --- a/go.mod +++ b/go.mod @@ -6,7 +6,7 @@ require ( github.com/gobwas/httphead v0.0.0-20180130184737-2c6c146eadee // indirect github.com/gobwas/pool v0.2.0 // indirect github.com/gobwas/ws v1.0.2 - github.com/gorilla/websocket v1.4.0 + github.com/gorilla/websocket v1.4.1 // indirect github.com/iris-contrib/go.uuid v2.0.0+incompatible github.com/mediocregopher/radix/v3 v3.3.0 github.com/nats-io/nats.go v1.8.1 // indirect diff --git a/go.sum b/go.sum index 94a0274..6df5976 100644 --- a/go.sum +++ b/go.sum @@ -4,14 +4,15 @@ github.com/gobwas/pool v0.2.0 h1:QEmUOlnSjWtnpRGHF3SauEiOsy82Cup83Vf2LcMlnc8= github.com/gobwas/pool v0.2.0/go.mod h1:q8bcK0KcYlCgd9e7WYLm9LpyS+YeLd8JVDW6WezmKEw= github.com/gobwas/ws v1.0.2 h1:CoAavW/wd/kulfZmSIBt6p24n4j7tHgNVCjsfHVNUbo= github.com/gobwas/ws v1.0.2/go.mod h1:szmBTxLgaFppYjEmNtny/v3w89xOydFnnZMcgRRu/EM= -github.com/gorilla/websocket v1.4.0 h1:WDFjx/TMzVgy9VdMMQi2K2Emtwi2QcUQsztZ/zLaH/Q= -github.com/gorilla/websocket v1.4.0/go.mod h1:E7qHFY5m1UJ88s3WnNqhKjPHQ0heANvMoAMk2YaljkQ= +github.com/gorilla/websocket v1.4.1 h1:q7AeDBpnBk8AogcD4DSag/Ukw/KV+YhzLj2bP5HvKCM= +github.com/gorilla/websocket v1.4.1/go.mod h1:YR8l580nyteQvAITg2hZ9XVh4b55+EU/adAjf1fMHhE= github.com/iris-contrib/go.uuid v2.0.0+incompatible h1:XZubAYg61/JwnJNbZilGjf3b3pB80+OQg2qf6c8BfWE= github.com/iris-contrib/go.uuid v2.0.0+incompatible/go.mod h1:iz2lgM/1UnEf1kP0L/+fafWORmlnuysV2EMP8MW+qe0= github.com/mediocregopher/radix/v3 v3.3.0 h1:oacPXPKHJg0hcngVVrdtTnfGJiS+PtwoQwTBZGFlV4k= github.com/mediocregopher/radix/v3 v3.3.0/go.mod h1:EmfVyvspXz1uZEyPBMyGK+kjWiKQGvsUt6O3Pj+LDCQ= github.com/nats-io/nats.go v1.8.1 h1:6lF/f1/NN6kzUDBz6pyvQDEXO39jqXcWRLu/tKjtOUQ= github.com/nats-io/nats.go v1.8.1/go.mod h1:BrFz9vVn0fU3AcH9Vn4Kd7W0NpJ651tD5omQ3M8LwxM= +github.com/nats-io/nkeys v0.0.2/go.mod h1:dab7URMsZm6Z/jp9Z5UGa87Uutgc2mVpXLC4B7TDb/4= github.com/nats-io/nkeys v0.1.0 h1:qMd4+pRHgdr1nAClu+2h/2a5F2TmKcCzjCDazVgRoX4= github.com/nats-io/nkeys v0.1.0/go.mod h1:xpnFELMwJABBLVhffcfd1MZx6VsNRFpEugbxziKVo7w= github.com/nats-io/nuid v1.0.1 h1:5iA8DT8V7q8WK2EScv2padNa/rTESc1KdnPw4TC2paw=