-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
3 changed files
with
73 additions
and
50 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 |
---|---|---|
@@ -1,34 +1,57 @@ | ||
//chat_client.ts | ||
|
||
function logError (msg: string) { | ||
console.log(msg); | ||
Deno.exit(1); | ||
} | ||
// function logError (msg: string) { | ||
// console.log(msg); | ||
// Deno.exit(1); | ||
// } | ||
|
||
// function handleConnected (ws: WebSocket) { | ||
// console.log('Connected to server ...'); | ||
// handleMessage(ws, 'Welcome!'); | ||
// } | ||
|
||
// function handleMessage (ws: WebSocket, data: string) { | ||
// console.log('SERVER >> ' + data); | ||
// const reply = prompt('Client >> ') || 'No reply'; | ||
// if (reply === 'exit') return ws.close(); | ||
// ws.send(reply as string); | ||
// } | ||
|
||
// function handleError (e: Event | ErrorEvent) { | ||
// console.log(e instanceof ErrorEvent ? e.message : e.type); | ||
// } | ||
|
||
function handleConnected (ws: WebSocket) { | ||
console.log("Connected to server ..."); | ||
handleMessage(ws, "Welcome!"); | ||
function open (e: Event) { | ||
const w = e.target as WebSocket; | ||
console.log('open'); | ||
w.send('foo'); | ||
} | ||
|
||
function handleMessage (ws: WebSocket, data: string) { | ||
console.log("SERVER >> " + data); | ||
const reply = prompt("Client >> ") || "No reply"; | ||
if (reply === "exit") return ws.close(); | ||
ws.send(reply as string); | ||
function close (e: Event) { | ||
const w = e.target as WebSocket; | ||
console.log('close'); | ||
} | ||
|
||
function handleError (e: Event | ErrorEvent) { | ||
console.log(e instanceof ErrorEvent ? e.message : e.type); | ||
function error (e: Event) { | ||
const w = e.target as WebSocket; | ||
console.log('error'); | ||
} | ||
|
||
console.log("Connecting to server ..."); | ||
function message (e: MessageEvent) { | ||
const w = e.target as WebSocket; | ||
console.log('client message: ', e.data); | ||
// w.send(new Date().toString()); | ||
} | ||
|
||
try { | ||
const ws = new WebSocket("ws://localhost:8000"); | ||
ws.onopen = () => handleConnected(ws); | ||
ws.onmessage = (m) => handleMessage(ws, m.data); | ||
ws.onclose = () => logError("Disconnected from server ..."); | ||
ws.onerror = (e) => handleError(e); | ||
} catch (err) { | ||
logError("Failed to connect to server ... exiting"); | ||
|
||
const ws = new WebSocket('ws://localhost:8000'); | ||
|
||
ws.onopen = open; | ||
ws.onclose = close; | ||
ws.onerror = error; | ||
ws.onmessage = message; | ||
|
||
// ws.send('client: open'); | ||
} catch (e) { | ||
console.log(e); | ||
} |
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
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