Permalink
Browse files

Complete Node SignalR Chat (TypeScript)

  • Loading branch information...
moozzyk committed Oct 1, 2017
1 parent b83cf92 commit 2a6d0e963db068df5085b08c408a2480a41d3007
Showing with 37 additions and 0 deletions.
  1. +37 −0 SignalRChatNode/index.ts
View
@@ -0,0 +1,37 @@
import * as readline from "readline"
import * as signalR from "@aspnet/signalr-client"
(<any>global).XMLHttpRequest = require("xmlhttprequest").XMLHttpRequest;
(<any>global).WebSocket = require("websocket").w3cwebsocket;
let rl = readline.createInterface(process.stdin, process.stdout);
rl.question('Enter your name: ', async name => {
console.log(name);
let connection = new signalR.HubConnection("http://localhost:5000/chat");
connection.on("broadcastMessage", (name, message) => {
console.log(`${name}: ${message}`);
rl.prompt(true);
});
try {
await connection.start();
rl.prompt();
rl.on("line", async input => {
if (input === "!q") {
console.log("Stopping connection...");
connection.stop();
rl.close();
return;
}
await connection.send("send", name, input);
});
}
catch (error) {
console.error(error);
rl.close();
}
});

0 comments on commit 2a6d0e9

Please sign in to comment.