-
Notifications
You must be signed in to change notification settings - Fork 582
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Tomasz Janczuk
authored and
Tomasz Janczuk
committed
Sep 11, 2012
1 parent
8184cc5
commit 73d1082
Showing
5 changed files
with
89 additions
and
1 deletion.
There are no files selected for viewing
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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,52 @@ | ||
/* | ||
A WebSocket connection receives 3 messages from the server | ||
*/ | ||
|
||
var WebSocket = require('faye-websocket') | ||
, iisnodeassert = require('iisnodeassert') | ||
, assert = require('assert'); | ||
|
||
var lines = ['The', 'cat', 'in', 'the', 'hat']; | ||
var address = iisnodeassert.getAddress('ws', '/131_websocket_echo/server.js'); | ||
//var address = 'ws://localhost:8888/'; | ||
var ws = new WebSocket.Client(address); | ||
var receiveIndex = 0; | ||
|
||
var timeout = setTimeout(function () { | ||
assert.fail('10s', '< 10s', 'The messages had not been echoed within 10s'); | ||
}, 10000); | ||
|
||
ws.onmessage = function (event) { | ||
console.log('onmessage: ' + event.data); | ||
assert.strictEqual(lines[receiveIndex], event.data); | ||
receiveIndex++; | ||
if (receiveIndex === lines.length) | ||
ws.close(); | ||
}; | ||
|
||
ws.onclose = function (event) { | ||
console.log('onclose'); | ||
clearTimeout(timeout); | ||
assert.strictEqual(lines.length, receiveIndex, 'All messages were received'); | ||
}; | ||
|
||
ws.onerror = function (error) { | ||
console.log('onerror'); | ||
assert.ifError(error); | ||
}; | ||
|
||
ws.onopen = function () { | ||
console.log('onopen'); | ||
var scheduleSendLine = function (line) { | ||
if (line < lines.length) | ||
setTimeout(function () { | ||
console.log('sending ' + lines[line]); | ||
ws.send(lines[line]); | ||
scheduleSendLine(line + 1); | ||
}, 200); | ||
}; | ||
|
||
console.log('sending ' + lines[0]); | ||
ws.send(lines[0]); | ||
scheduleSendLine(1); | ||
}; |
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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
|
||
var WebSocket = require('faye-websocket') | ||
, http = require('http'); | ||
|
||
var server = http.createServer(function (req, res) { | ||
res.writeHead(400); | ||
res.end(); | ||
}); | ||
|
||
server.addListener('upgrade', function (request, socket, head) { | ||
var ws = new WebSocket(request, socket, head); | ||
|
||
ws.onmessage = function (event) { | ||
if (ws) | ||
ws.send(event.data); | ||
}; | ||
|
||
ws.onclose = function (event) { | ||
ws = null; | ||
}; | ||
}); | ||
|
||
server.listen(process.env.PORT || 8888); |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
<configuration> | ||
<system.webServer> | ||
<handlers> | ||
<add name="iisnode" path="server.js" verb="*" modules="iisnode" /> | ||
</handlers> | ||
<webSocket enabled="false" /> | ||
</system.webServer> | ||
</configuration> |