PageRenderTime 225ms CodeModel.GetById 100ms app.highlight 52ms RepoModel.GetById 65ms app.codeStats 1ms

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

http://github.com/onedayitwillmake/RealtimeMultiplayerNodeJs
JavaScript | 63 lines | 49 code | 12 blank | 2 comment | 0 complexity | e9b409aeaab819dae8f17a867fc2ccf8 MD5 | raw file
 1// Verify that both sides of the WS connection can both send and receive file
 2// descriptors.
 3
 4var assert = require('assert');
 5var fs = require('fs');
 6var path = require('path');
 7var sys = require('sys');
 8var WebSocket = require('../lib/websocket').WebSocket;
 9var WebSocketServer = require('websocket-server/ws').Server;
10
11var PATH = path.join(__dirname, 'sock.' + process.pid);
12var C_MSG = 'Client test: ' + (Math.random() * 100);
13var S_MSG = 'Server test: ' + (Math.random() * 100);
14
15var clientReceivedData = false;
16var clientReceivedFD = false;
17var serverReceivedData = false;
18var serverReceivedFD = false;
19
20var wss = new WebSocketServer();
21wss.addListener('listening', function() {
22    var ws = new WebSocket('ws+unix://' + PATH);
23    ws.addListener('data', function(d) {
24        assert.equal(d.toString('utf8'), S_MSG);
25
26        clientReceivedData = true;
27
28        ws.send(C_MSG, 1);
29        ws.close();
30    });
31    ws.addListener('fd', function(fd) {
32        assert.ok(fd >= 0);
33
34        clientReceivedFD = true;
35    });
36});
37wss.addListener('connection', function(c) {
38    c.write(S_MSG, 0);
39    c._req.socket.addListener('fd', function(fd) {
40        assert.ok(fd >= 0);
41
42        serverReceivedFD = true;
43    });
44    c.addListener('message', function(d) {
45        assert.equal(d, C_MSG);
46
47        serverReceivedData = true;
48
49        wss.close();
50    });
51});
52wss.listen(PATH);
53
54process.addListener('exit', function() {
55    assert.ok(clientReceivedFD);
56    assert.ok(clientReceivedData);
57    assert.ok(serverReceivedFD);
58    assert.ok(serverReceivedData);
59
60    try {
61        fs.unlinkSync(PATH);
62    } catch (e) { }
63});