Created
September 22, 2020 05:38
-
-
Save deleteman/436300813ef706b5a816fb5c997ecb51 to your computer and use it in GitHub Desktop.
This file contains hidden or 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
const decoder = new TextDecoder('utf-8'); | |
let text:string; | |
let lines:string[]; | |
try { | |
text = decoder.decode(await Deno.readFile("./test.txt")) | |
lines = text.split("\n") | |
} catch(e) { | |
console.error(e) | |
Deno.exit(1) | |
} | |
// Start the connection to the WebSocket server at echo.websocket.org | |
const ws = new WebSocket("ws://echo.websocket.org/"); | |
// Register event listeners for the open, close, and message events | |
ws.onopen = () => { | |
console.log("WebSocket ready!"); | |
lines.forEach( (l:string) => { | |
console.log(l) | |
ws.send(l) | |
} ) | |
}; | |
ws.onmessage = (message) => { | |
// Log the message we recieve: | |
console.log("Received data:", message.data); | |
// Close the websocket after receiving the "exit" message (the last line) | |
if(message.data == "exit") ws.close(); | |
}; | |
ws.onclose = () => console.log("WebSocket closed!"); | |
ws.onerror = (err) => console.log("WebSocket error:", err); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment