PageRenderTime 22ms CodeModel.GetById 13ms app.highlight 7ms RepoModel.GetById 1ms app.codeStats 0ms

/js/lib/Socket.IO-node/support/node-websocket-client/test/test-unix-sockets.js

http://github.com/onedayitwillmake/RealtimeMultiplayerNodeJs
JavaScript | 46 lines | 37 code | 8 blank | 1 comment | 0 complexity | a609fcacbb363459faa91526d56d9622 MD5 | raw file
 1// Verify that we can connect to a server over UNIX domain sockets.
 2
 3var assert = require('assert');
 4var fs = require('fs');
 5var path = require('path');
 6var sys = require('sys');
 7var WebSocket = require('../lib/websocket').WebSocket;
 8var WebSocketServer = require('websocket-server/ws').Server;
 9
10var PATH = path.join(__dirname, 'sock.' + process.pid);
11var S_MSG = 'Server test: ' + (Math.random() * 100);
12
13var serverGotConnection = false;
14var clientGotOpen = false;
15var clientGotData = false;
16
17var wss = new WebSocketServer();
18wss.addListener('listening', function() {
19    var ws = new WebSocket('ws+unix://' + PATH);
20    ws.addListener('open', function() {
21        clientGotOpen = true;
22
23        ws.close();
24    });
25    ws.addListener('data', function(d) {
26        assert.equal(d.toString('utf8'), S_MSG);
27        clientGotData = true;
28    });
29});
30wss.addListener('connection', function(c) {
31    serverGotConnection = true;
32
33    c.write(S_MSG);
34    wss.close();
35});
36wss.listen(PATH);
37
38process.addListener('exit', function() {
39    assert.ok(serverGotConnection);
40    assert.ok(clientGotOpen);
41    assert.ok(clientGotData);
42
43    try {
44        fs.unlinkSync(PATH);
45    } catch(e) { }
46});