Created
December 27, 2011 18:38
-
-
Save zslayton/1524725 to your computer and use it in GitHub Desktop.
Demo client & server for issue #359
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 io_client = require('socket.io-client'); | |
host = 'localhost'; | |
port = 8001; | |
var url = "http://" + host + ":" + port; | |
var client = io_client.connect(url); | |
console.log("Attempting to connect with server@" + host + ":" + port); | |
function on_client_connect() | |
{ | |
console.log("Contact with server@" + host + ":" + port + " established."); | |
} | |
function on_client_message(data) | |
{ | |
console.log("Generic message received:", data); | |
} | |
function on_client_disconnect() | |
{ | |
console.warn("Contact with server@" + host + ":" + port + " lost."); | |
} | |
function on_client_close() | |
{ | |
console.warn("Server closed the connection."); | |
} | |
function on_client_connect_failed(err) | |
{ | |
console.error("Failed to connect to server@" + host + ":" + port + ". Error: " + err); | |
} | |
client.on('connect', on_client_connect); | |
client.on('connect_failed', on_client_connect_failed); | |
client.on('message', on_client_message); | |
client.on('disconnect', on_client_disconnect); | |
client.on('close', on_client_close); |
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
#!/usr/bin/env node | |
var http = require('http'); | |
var io = require('socket.io'); | |
var utils= require('./utils'); | |
// BASIC WEB SERVER | |
function on_request(request, response) | |
{ | |
response.writeHead(401); | |
return response.end('Kindly scram.'); | |
} | |
var web_server = http.createServer(on_request); | |
// WEB SOCKET SERVER | |
var ws_server = io.listen(web_server); | |
var ws_clients = []; | |
ws_server.set('log level', 2); | |
try | |
{ | |
web_server.listen(8001); | |
} | |
catch(web_server_error) | |
{ | |
console.log("Unable to initialize webserver: " + web_server_error); | |
process.exit(1); | |
} | |
function on_ws_connect(client) | |
{ | |
ws_clients.push(client); | |
function on_ws_message(event) | |
{ | |
console.log('Received:', event); | |
} | |
function on_ws_disconnect() | |
{ | |
console.log('Disconnected.'); | |
if(utils.remove_from_array(client, ws_clients)) | |
{ | |
console.log("Client removed from active WS Clients."); | |
} | |
else | |
{ | |
console.log("Couldn't remove client from active client pool."); | |
} | |
} | |
client.on('message', on_ws_message); | |
client.on('disconnect', on_ws_disconnect); | |
} | |
ws_server.sockets.on('connection', on_ws_connect); |
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
exports.remove_from_array = | |
function (the_item, the_array) | |
{ | |
for(var m = 0; m < the_array.length; m++) | |
{ | |
if(the_array[m] == the_item) | |
{ | |
the_array.splice(m, 1); | |
return true; | |
} | |
} | |
return false; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment