Merge branch 'master' of git+ssh://source.hacker.lu/var/repo/projects/syndilights
commit
86190027c1
|
@ -6,25 +6,26 @@ var http = require('http');
|
||||||
var server = http.createServer(function(request, response) {});
|
var server = http.createServer(function(request, response) {});
|
||||||
|
|
||||||
server.listen(1234, function() {
|
server.listen(1234, function() {
|
||||||
console.log((new Date()) + ' Server is listening on port 1234');
|
console.log((new Date()) + ' Server is listening on port 1234');
|
||||||
});
|
});
|
||||||
|
|
||||||
var WebSocketServer = require('websocket').server;
|
var WebSocketServer = require('websocket').server;
|
||||||
wsServer = new WebSocketServer({
|
wsServer = new WebSocketServer({
|
||||||
httpServer: server
|
httpServer: server
|
||||||
});
|
});
|
||||||
|
|
||||||
wsServer.on('request', function(r){
|
wsServer.on('request', function(r) {
|
||||||
|
|
||||||
var connection = r.accept('echo-protocol', r.origin);
|
var connection = r.accept('echo-protocol', r.origin);
|
||||||
|
|
||||||
// Specific id for this client & increment count
|
// Specific id for this client & increment count
|
||||||
var id = count++;
|
var id = count++;
|
||||||
|
|
||||||
// // Store the connection method so we can loop through & contact all clients
|
// Store the connection method so we can loop through & contact all clients
|
||||||
clients[id] = connection
|
clients[id] = connection
|
||||||
|
|
||||||
console.log((new Date()) + ' Connection accepted [' + id + ']');
|
console.log((new Date()) + ' Connection accepted [' + id + ']');
|
||||||
|
clients[id].sendUTF("Welcome to the server. You are connected. This message has been pushed to you.");
|
||||||
|
|
||||||
// Create event listener
|
// Create event listener
|
||||||
connection.on('message', function(message) {
|
connection.on('message', function(message) {
|
||||||
|
@ -41,10 +42,27 @@ wsServer.on('request', function(r){
|
||||||
|
|
||||||
// Create event listener for close
|
// Create event listener for close
|
||||||
connection.on('close', function(reasonCode, description) {
|
connection.on('close', function(reasonCode, description) {
|
||||||
delete clients[id];
|
delete clients[id];
|
||||||
console.log((new Date()) + ' Peer ' + connection.remoteAddress + ' disconnected.');
|
console.log((new Date()) + ' Peer ' + connection.remoteAddress + ' disconnected.');
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
|
||||||
|
var sendTime = function () {
|
||||||
|
var now, i = 0;
|
||||||
|
|
||||||
|
// get time now
|
||||||
|
now = new Date();
|
||||||
|
|
||||||
|
// send time to all clients
|
||||||
|
for(i in clients) {
|
||||||
|
// Send a message to the client with the message
|
||||||
|
clients[i].sendUTF(now);
|
||||||
|
}
|
||||||
|
|
||||||
|
// repeat in 5 seconds
|
||||||
|
setTimeout(sendTime, 5000);
|
||||||
|
};
|
||||||
|
|
||||||
|
|
||||||
|
// every 5 seconds send the date/time
|
||||||
|
setTimeout(sendTime, 5000);
|
||||||
|
|
|
@ -4,20 +4,28 @@
|
||||||
<meta charset="utf-8">
|
<meta charset="utf-8">
|
||||||
<title>Web socket test</title>
|
<title>Web socket test</title>
|
||||||
<script type="text/javascript">
|
<script type="text/javascript">
|
||||||
var ws = new WebSocket('ws://localhost:1234', 'echo-protocol');
|
|
||||||
|
var ws;
|
||||||
|
|
||||||
function sendMessage(){
|
function sendMessage(){
|
||||||
var message = document.getElementById('message').value;
|
var message = document.getElementById('message').value;
|
||||||
ws.send(message);
|
ws.send(message);
|
||||||
}
|
}
|
||||||
|
|
||||||
ws.addEventListener("message", function(e) {
|
function connection() {
|
||||||
// The data is simply the message that we're sending back
|
ws = new WebSocket('ws://localhost:1234', 'echo-protocol');
|
||||||
var msg = e.data;
|
|
||||||
|
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;
|
||||||
|
});
|
||||||
|
|
||||||
|
document.getElementById('connect_button').style.display = 'none';
|
||||||
|
}
|
||||||
|
|
||||||
// Append the message
|
|
||||||
document.getElementById('chatlog').innerHTML += '<br>' + msg;
|
|
||||||
});
|
|
||||||
|
|
||||||
</script>
|
</script>
|
||||||
</head>
|
</head>
|
||||||
|
@ -27,5 +35,6 @@
|
||||||
<input type='button' value='send message' onclick='sendMessage()'/>
|
<input type='button' value='send message' onclick='sendMessage()'/>
|
||||||
<div>Received from server:</div>
|
<div>Received from server:</div>
|
||||||
<div id='chatlog'></div>
|
<div id='chatlog'></div>
|
||||||
|
<input id='connect_button' type='button' value='Connect to server using websockets' onclick='connection()'/>
|
||||||
</body>
|
</body>
|
||||||
</html>
|
</html>
|
||||||
|
|
Loading…
Reference in New Issue