iris/_examples/websocket/basic/nodejs-client/client.js
Gerasimos (Makis) Maropoulos 272566950d _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
2019-06-13 12:15:01 +03:00

36 lines
935 B
JavaScript

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();