Skip to content

Commit

Permalink
update gorilla's websocket module - #18
Browse files Browse the repository at this point in the history
  • Loading branch information
kataras committed Oct 16, 2019
1 parent a36e27c commit 6583461
Show file tree
Hide file tree
Showing 3 changed files with 42 additions and 18 deletions.
53 changes: 38 additions & 15 deletions _examples/basic/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -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`.
Expand All @@ -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() {
Expand All @@ -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() {
Expand All @@ -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)
Expand All @@ -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")
Expand Down Expand Up @@ -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, ">> ")
Expand All @@ -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, ">> ")
}
}
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
5 changes: 3 additions & 2 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -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=
Expand Down

0 comments on commit 6583461

Please sign in to comment.