This repository has been archived by the owner on Feb 13, 2019. It is now read-only.
-
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.
improvements to demo, safe write message
- Loading branch information
1 parent
2de93f8
commit 3212e8c
Showing
7 changed files
with
97 additions
and
66 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,56 @@ | ||
$(document).ready(function(){ | ||
if (!('WebSocket' in window)) { | ||
alert('WebSockets are not supported by your browser.'); | ||
return; | ||
} | ||
|
||
var ws = null; | ||
|
||
function close(){ | ||
log('closing connection...'); | ||
ws.close(); | ||
ws = null; | ||
} | ||
$('#close').click(close); | ||
|
||
function open(){ | ||
if (ws !== null){ | ||
close(); | ||
} | ||
var url = djws.ws_url; | ||
log('connecting to "' + url + '" with token "' + djws.token + '"...'); | ||
ws = new WebSocket(url, djws.token); | ||
|
||
ws.onopen = function(){ | ||
log('connected'); | ||
}; | ||
|
||
ws.onmessage = function (evt) { | ||
log('< ' + evt.data); | ||
}; | ||
|
||
ws.onclose = function (evt) { | ||
log('Connection closed, reason: "' + evt.reason + '", code: ' + evt.code); | ||
console.log('close event:', evt); | ||
}; | ||
} | ||
$('#open').click(open); | ||
open(); | ||
|
||
$('#user-input').submit(function (e){ | ||
e.preventDefault(); | ||
var msg = $msg_box.val(); | ||
log('> '+ msg); | ||
ws.send(msg); | ||
$msg_box.val(''); | ||
}); | ||
}); | ||
|
||
var $console = $('#console'); | ||
var $msg_box = $('#message-box'); | ||
|
||
function log(message){ | ||
$console.append(message + '\n'); | ||
console.log(message); | ||
$console[0].scrollTop = $console.height(); | ||
} |
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
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
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 was deleted.
Oops, something went wrong.