mirror of
https://github.com/kataras/iris.git
synced 2025-12-19 02:47:04 +00:00
_examples/websocket/basic: add a nodejs client and provide a README.md on how to run the websocket clients and the server
Former-commit-id: a98a80996d7d95fa947e72c71803407682229fa7
This commit is contained in:
35
_examples/websocket/basic/nodejs-client/client.js
Normal file
35
_examples/websocket/basic/nodejs-client/client.js
Normal file
@@ -0,0 +1,35 @@
|
||||
const neffos = require('neffos.js');
|
||||
const stdin = process.openStdin();
|
||||
|
||||
const wsURL = "ws://localhost:8080/echo";
|
||||
|
||||
async function runExample() {
|
||||
try {
|
||||
const conn = await neffos.dial(wsURL, {
|
||||
default: { // "default" namespace.
|
||||
_OnNamespaceConnected: function (nsConn, msg) {
|
||||
console.log("connected to namespace: " + msg.Namespace);
|
||||
},
|
||||
_OnNamespaceDisconnect: function (nsConn, msg) {
|
||||
console.log("disconnected from namespace: " + msg.Namespace);
|
||||
},
|
||||
chat: function (nsConn, msg) { // "chat" event.
|
||||
console.log(msg.Body);
|
||||
}
|
||||
}
|
||||
});
|
||||
|
||||
const nsConn = await conn.connect("default");
|
||||
nsConn.emit("chat", "Hello from Nodejs client side!");
|
||||
|
||||
stdin.addListener("data", function (data) {
|
||||
const text = data.toString().trim();
|
||||
nsConn.emit("chat", text);
|
||||
});
|
||||
|
||||
} catch (err) {
|
||||
console.error(err);
|
||||
}
|
||||
}
|
||||
|
||||
runExample();
|
||||
Reference in New Issue
Block a user