Skip to content

Commit

Permalink
add NSConn#EmitBinary helper (js: emitBinary)
Browse files Browse the repository at this point in the history
  • Loading branch information
kataras committed Dec 10, 2019
1 parent 7c3c769 commit eb5c6fb
Show file tree
Hide file tree
Showing 5 changed files with 7,803 additions and 18 deletions.
16 changes: 9 additions & 7 deletions _examples/protobuf/browser/app.js
Original file line number Diff line number Diff line change
Expand Up @@ -31,16 +31,18 @@ function handleNamespaceConnectedConn(nsConn) {
userMsg.setText(input);

const body = userMsg.serializeBinary()
let msg = new neffos.Message();
msg.Namespace = "default";
msg.Event = "chat";
msg.Body = body;
msg.SetBinary = true;
nsConn.conn.write(msg);
// let msg = new neffos.Message();
// msg.Namespace = "default";
// msg.Event = "chat";
// msg.Body = body;
// msg.SetBinary = true;
// nsConn.conn.write(msg);
// ^ ==
nsConn.emitBinary("chat", body);
//
// OR: javascript side will check if body is binary,
// and if it's it will convert it to valid utf-8 text before sending.
// To keep the data as they are, please prefer the above commented code (msg.SetBinary = true).
// To keep the data as they are, please prefer `emitBinary`.
// nsConn.emit("chat", body);
addMessage("Me: " + input);
};
Expand Down
7,782 changes: 7,780 additions & 2 deletions _examples/protobuf/browser/bundle.js

Large diffs are not rendered by default.

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": "^3.11.1",
"neffos.js": "^0.1.24",
"neffos.js": "^0.1.25",
"protobufjs": "~6.8.8"
},
"devDependencies": {
Expand Down
9 changes: 2 additions & 7 deletions _examples/protobuf/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -169,7 +169,7 @@ func startClient() {
userMsg := &UserMessage{
Username: string(usernameBytes),
// only `Text` field is dynamic, therefore we can reuse this instance value,
// the `Text` field can be filled right before the `Conn#Write`, check below.
// the `Text` field can be filled right before the `c#EmitBinary`, check below.
}

fmt.Fprint(os.Stdout, ">> ")
Expand Down Expand Up @@ -199,12 +199,7 @@ func startClient() {
log.Fatal(err)
}

c.Conn.Write(neffos.Message{
Namespace: namespace,
Event: "chat",
Body: body,
SetBinary: true,
})
c.EmitBinary("chat", body)

fmt.Fprint(os.Stdout, ">> ")
}
Expand Down
12 changes: 11 additions & 1 deletion conn_namespace.go
Original file line number Diff line number Diff line change
Expand Up @@ -49,13 +49,23 @@ func (ns *NSConn) String() string {
// Emit method sends a message to the remote side
// with its `Message.Namespace` filled to this specific namespace.
func (ns *NSConn) Emit(event string, body []byte) bool {
if ns == nil { // if for any reason Namespace() called without be available.
if ns == nil {
return false
}

return ns.Conn.Write(Message{Namespace: ns.namespace, Event: event, Body: body})
}

// EmitBinary acts like `Emit` but it sets the `Message.SetBinary` to true
// and sends the data as binary, the receiver's Message in javascript-side is Uint8Array.
func (ns *NSConn) EmitBinary(event string, body []byte) bool {
if ns == nil {
return false
}

return ns.Conn.Write(Message{Namespace: ns.namespace, Event: event, Body: body, SetBinary: true})
}

// Ask method writes a message to the remote side and blocks until a response or an error received.
func (ns *NSConn) Ask(ctx context.Context, event string, body []byte) (Message, error) {
if ns == nil {
Expand Down

0 comments on commit eb5c6fb

Please sign in to comment.