Permalink
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Showing
1 changed file
with
37 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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(); | ||
} | ||
}); |