Created
June 23, 2017 21:37
-
-
Save davidferguson/e245b5030f43aa723f54f181ea925249 to your computer and use it in GitHub Desktop.
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
var websocket | |
function doConnect() | |
{ | |
websocket = new WebSocket('ws://localhost:8000/') | |
websocket.onopen = function(evt) { onOpen(evt) } | |
websocket.onclose = function(evt) { onClose(evt) } | |
websocket.onmessage = function(evt) { onMessage(evt) } | |
websocket.onerror = function(evt) { onError(evt) } | |
} | |
function onOpen(evt) { | |
console.log('websocket opened') | |
} | |
function onClose(evt) { | |
console.log('websocket closed') | |
} | |
function onMessage(evt) { | |
var data = evt.data | |
// do whatever you want here | |
} | |
function onError(evt) { | |
var error = evt.data | |
console.log('websocket error', error); | |
websocket.close(); | |
} | |
function sendMessage(message) { | |
websocket.send(message); | |
} | |
function doDisconnect() { | |
websocket.close(); | |
} | |
// call doConnect() to connect, doDisconnect() to disconnect, sendMessage(data) to send a message, and add your receiver code to onMessage |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment