Skip to content

Commit

Permalink
minor: examples
Browse files Browse the repository at this point in the history
  • Loading branch information
kataras committed Jan 25, 2023
1 parent 54cbc4d commit c35565a
Show file tree
Hide file tree
Showing 19 changed files with 167 additions and 184 deletions.
File renamed without changes.
File renamed without changes.
File renamed without changes.
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
"build": "npm run-script browserify && npm run-script minifyES6"
},
"dependencies": {
"neffos.js": "^0.1.31"
"neffos.js": "^0.1.32"
},
"devDependencies": {
"babel-minify": "^0.5.2",
Expand Down
12 changes: 6 additions & 6 deletions _examples/basic/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ var serverAndClientEvents = neffos.Namespaces{
log.Printf("[%s] connected to namespace [%s].", c, msg.Namespace)

if !c.Conn.IsClient() && serverJoinRoom {
c.JoinRoom(nil, serverRoomName)
c.JoinRoom(context.TODO(), serverRoomName)
}

return nil
Expand Down Expand Up @@ -171,8 +171,8 @@ func startServer() {
log.Printf("[%s] disconnected from the server.", c)
}

log.Printf("Listening on: %s\nPress CTRL/CMD+C to interrupt.", addr)
http.Handle("/", http.FileServer(http.Dir("./browser")))
log.Printf("Listening on: http://%s/browserify\nPress CTRL/CMD+C to interrupt.", addr)
http.Handle("/", http.FileServer(http.Dir("./browserify")))
http.Handle(endpoint, server)
log.Fatal(http.ListenAndServe(addr, nil))
}
Expand Down Expand Up @@ -219,7 +219,7 @@ func startClient() {
}()

// connect to the "default" namespace.
c, err := client.Connect(nil, namespace)
c, err := client.Connect(context.TODO(), namespace)
if err != nil {
log.Fatal(err)
}
Expand All @@ -236,7 +236,7 @@ askRoom:
}
roomToJoin := scanner.Text()

room, err = c.JoinRoom(nil, roomToJoin)
room, err = c.JoinRoom(context.TODO(), roomToJoin)
if err != nil {
log.Fatal(err)
}
Expand All @@ -262,7 +262,7 @@ askRoom:
}

if text == "leave" {
room.Leave(nil)
room.Leave(context.TODO())
if !serverJoinRoom {
goto askRoom
}
Expand Down
2 changes: 1 addition & 1 deletion _examples/browser/README.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
# Client-side for browser and nodejs

Navigate to <https://github.com/kataras/neffos> and <https://github.com/kataras/neffos.js> instead.
Navigate through <https://github.com/kataras/neffos.js>.
29 changes: 12 additions & 17 deletions _examples/example/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -38,13 +38,12 @@ var handler = neffos.WithTimeout{
return nil
}

err := fmt.Errorf("Server says that you are not allowed here")
/* comment this to see that the server-side will
/* Uncomment this to see that the server-side will
no allow to for this socket to be connected to the "default" namespace
and an error will be logged to the client. */
err = nil
// return fmt.Errorf("Server says that you are not allowed here")

return err
return nil
},
neffos.OnNamespaceConnected: func(c *neffos.NSConn, msg neffos.Message) error {
if !c.Conn.IsClient() {
Expand All @@ -61,22 +60,18 @@ var handler = neffos.WithTimeout{
return nil
}

err := fmt.Errorf("Server says that you are not allowed to be disconnected yet")
/* here if you comment this, the return error will mean that
/* here if you uncomment this, the return error will mean that
the disconnect message from client-side will be ignored from the server
and the connection would be still available to send message to the "default" namespace
it will not be disconnected.*/
err = nil

if err == nil {
log.Printf("[%s] disconnected from [%s].", c.Conn.ID(), msg.Namespace)
}
// return fmt.Errorf("Server says that you are not allowed to be disconnected yet")

log.Printf("[%s] disconnected from [%s].", c.Conn.ID(), msg.Namespace)
if c.Conn.IsClient() {
os.Exit(0)
}

return err
return nil
},
"chat": func(c *neffos.NSConn, msg neffos.Message) error {
if !c.Conn.IsClient() {
Expand Down Expand Up @@ -158,7 +153,7 @@ func server(upgrader neffos.Upgrader) {
log.Printf("[%s] connected to server.", c.ID())

if serverHandlesConnectNamespace {
ns, err := c.Connect(nil, namespace)
ns, err := c.Connect(context.TODO(), namespace)
if err != nil {
panic(err)
}
Expand Down Expand Up @@ -205,7 +200,7 @@ func server(upgrader neffos.Upgrader) {
text := scanner.Bytes()
if bytes.Equal(text, []byte("force disconnect")) {
srv.Do(func(c *neffos.Conn) {
c.DisconnectAll(nil)
c.DisconnectAll(context.TODO())
// c.Namespace(namespace).Disconnect(nil)
}, false)
} else {
Expand Down Expand Up @@ -254,9 +249,9 @@ func client(dialer neffos.Dialer) {
text := scanner.Bytes()

if bytes.Equal(text, []byte("exit")) {
if err := c.Disconnect(nil); err != nil {
// log.Printf("from server: %v", err)
}
// if err := c.Disconnect(context.TODO()); err != nil {
// log.Printf("from server: %v", err)
// }
continue
}

Expand Down
2 changes: 1 addition & 1 deletion _examples/protobuf/browser/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
},
"dependencies": {
"google-protobuf": "latest",
"neffos.js": "^0.1.31",
"neffos.js": "^0.1.32",
"protobufjs": "latest"
},
"devDependencies": {
Expand Down
9 changes: 5 additions & 4 deletions _examples/protobuf/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package main
import (
"bufio"
"bytes"
"context"
"fmt"
"log"
"net/http"
Expand All @@ -11,11 +12,11 @@ import (
"github.com/kataras/neffos"
"github.com/kataras/neffos/gorilla"

"github.com/golang/protobuf/proto"
"google.golang.org/protobuf/proto"
)

// Generate proto for Go:
// protoc --go_out=. user_message.proto
// protoc --go_out=. --go_opt=paths=source_relative user_message.proto
// go build
//
// Windows
Expand Down Expand Up @@ -136,7 +137,7 @@ const testHelloBinaryWithSeps = false

func startClient() {
// init the websocket connection by dialing the server.
client, err := neffos.Dial(nil, gorilla.DefaultDialer, addr+endpoint, serverAndClientEvents)
client, err := neffos.Dial(context.TODO(), gorilla.DefaultDialer, addr+endpoint, serverAndClientEvents)
if err != nil {
log.Fatal(err)
}
Expand All @@ -147,7 +148,7 @@ func startClient() {
}()

// connect to the "default" namespace.
c, err := client.Connect(nil, namespace)
c, err := client.Connect(context.TODO(), namespace)
if err != nil {
log.Fatal(err)
}
Expand Down
178 changes: 122 additions & 56 deletions _examples/protobuf/user_message.pb.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading

0 comments on commit c35565a

Please sign in to comment.