Created
November 24, 2013 08:39
-
-
Save twmht/7624900 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
<!doctype html> | |
<html> | |
<head> | |
<meta charset = "utf-8"> | |
<link rel = "stylesheet" href = "lounge.css"> | |
<script src = "lounge.js"></script> | |
</head> | |
<body> | |
<div id = "chatlog"></div> | |
<input type = "text" id = "message"></input> | |
<input type = "button" onclick = "sendMessage()"></input> | |
<script> | |
var ws = new WebSocket('ws://127.0.0.1:1234', 'echo-protocol') | |
ws.onopen = function (evt) { onOpen(evt) }; | |
ws.onclose = function (evt) { onClose(evt) }; | |
ws.onmessage = function (evt) { onMessage(evt) }; | |
ws.onerror = function (evt) { onError(evt) }; | |
function onOpen(evt) { | |
Log("Connected to WebSocket server."); | |
console.log("Connected to WebSocket server."); | |
} | |
function onClose(evt) { | |
console.log("Disconnected"); | |
} | |
function onMessage(evt) { | |
console.log('Retrieved data from server: ' + evt.data); | |
} | |
function onError(evt) { | |
console.log('Error occured: ' + evt.data); | |
} | |
function sendMessage(){ | |
var message = document.getElementById('message').value; | |
ws.send(message); | |
} | |
ws.addEventListener("message", function(e) { | |
// The data is simply the message that we're sending back | |
var msg = e.data; | |
// Append the message | |
document.getElementById('chatlog').innerHTML += '<br>' + msg; | |
}); | |
</script> | |
</body> | |
</html> |
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
var http = require('http'); | |
var server = http.createServer(function(request, response) {}); | |
server.listen(1234, function() { | |
console.log((new Date()) + ' Server is listening on port 1234'); | |
}); | |
var WebSocketServer = require('websocket').server; | |
wsServer = new WebSocketServer({ | |
httpServer: server | |
}); | |
wsServer.on('request', function(r){ | |
// Code here to run on connection | |
var connection = r.accept('echo-protocol', r.origin); | |
var count = 0; | |
var clients = {}; | |
// Specific id for this client & increment count | |
var id = count++; | |
// Store the connection method so we can loop through & contact all clients | |
clients[id] = connection | |
console.log((new Date()) + ' Connection accepted [' + id + ']'); | |
// Create event listener | |
connection.on('message', function(message) { | |
// The string message that was sent to us | |
var msgString = message.utf8Data; | |
// Loop through all clients | |
for(var i in clients){ | |
// Send a message to the client with the message | |
clients[i].sendUTF(msgString); | |
} | |
}); | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment