-
Notifications
You must be signed in to change notification settings - Fork 0
/
http_websocket_server.js
69 lines (54 loc) · 1.44 KB
/
http_websocket_server.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
/* ------------------------------------------
node.js v0.6.15 (http:https://nodejs.org/)
2012-04-10 13:05:32 -0600
Servidor web que recibe parametros por get y los envía mediante WebSocket al cliente.
@paulomcnally
------------------------------------------ */
/* ------------------------------------------
WebSocket
socket.io
http:https://socket.io
------------------------------------------ */
var WebSocket = require("socket.io").listen( 6969 );
WebSocket.sockets.on("connection", ws_start);
function ws_start(data)
{
data.on("new_message", ws_sendData);
}
function ws_sendData(data)
{
WebSocket.sockets.emit("ws_getData",data);
}
/* ------------------------------------------
WebServer
------------------------------------------ */
//var sys = require ('sys'),
var sys = require(process.binding('natives').util ? 'util' : 'sys'),
url = require('url'),
http = require('http'),
querystring = require('querystring');
http.createServer(function (req, res)
{
switch( req.method )
{
case 'GET':
var GET = url.parse(req.url,true);
if( GET.search != "" )
{
ws_sendData( GET.query );
}
console.log(GET.query);
break;
case 'POST':
req.addListener('data', function(chunk)
{
var POST = querystring.parse(chunk);
ws_sendData( POST );
console.log(POST);
});
break;
}
// Close load page
res.writeHead(200, {'Content-Type': 'text/plain'});
res.end('{"status":true}\n');
}).listen( 9391 );