Created
August 27, 2011 15:44
-
-
Save dakatsuka/1175522 to your computer and use it in GitHub Desktop.
WebSocket-Node Client Connection Test
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 vows = require('vows'); | |
var assert = require('assert'); | |
var events = require('events'); | |
var WebSocketClient = require('websocket').client; | |
vows.describe('WebSocket-Node Client Connection Test').addBatch({ | |
'HTTP': { | |
topic: function() { | |
var promise = new (events.EventEmitter); | |
var client = new WebSocketClient(); | |
client.on('connect', function(connection) { | |
connection.sendUTF("foobar"); | |
connection.on('message', function(message) { | |
promise.emit('success', message); | |
connection.close(); | |
}); | |
}); | |
client.connect("ws://localhost:8080"); | |
return promise; | |
}, | |
'should receive message': function(message) { | |
assert.equal(message.utf8Data, 'foobar'); | |
} | |
}, | |
'HTTPS': { | |
topic: function() { | |
var promise = new (events.EventEmitter); | |
var client = new WebSocketClient(); | |
client.on('connect', function(connection) { | |
connection.sendUTF("foobar"); | |
connection.on('message', function(message) { | |
promise.emit('success', message); | |
connection.close(); | |
}); | |
}); | |
client.connect("wss://localhost:8081"); | |
return promise; | |
}, | |
'should receive message': function(message) { | |
assert.equal(message.utf8Data, 'foobar'); | |
} | |
} | |
}).export(module); |
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 fs = require('fs'); | |
var http = require('http'); | |
var https = require('https'); | |
var WebSocketServer = require('websocket').server; | |
var server = http.createServer(); | |
var serverTLS = https.createServer({ | |
key: fs.readFileSync(__dirname + '/ssl.key'), | |
cert: fs.readFileSync(__dirname + '/ssl.crt') | |
}); | |
server.listen(8080); | |
serverTLS.listen(8081); | |
var ws = new WebSocketServer({ httpServer: server, autoAcceptConnections: true }); | |
var wss = new WebSocketServer({ httpServer: serverTLS, autoAcceptConnections: true }); | |
ws.on('connect', function(connection) { | |
console.log((new Date()) + " Connection accepted."); | |
connection.on('message', function(message) { | |
console.log("Received Message: " + message.utf8Data); | |
connection.sendUTF(message.utf8Data); | |
}); | |
connection.on('close', function(connection) { | |
console.log((new Date()) + " Disconnected"); | |
}); | |
}); | |
wss.on('connect', function(connection) { | |
console.log((new Date()) + " Connection accepted."); | |
connection.on('message', function(message) { | |
console.log("Received Message: " + message.utf8Data); | |
connection.sendUTF(message.utf8Data); | |
}); | |
connection.on('close', function(connection) { | |
console.log((new Date()) + " Disconnected"); | |
}); | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment