Skip to content

Instantly share code, notes, and snippets.

@Siedrix
Created December 18, 2011 02:38
Show Gist options
  • Save Siedrix/1492190 to your computer and use it in GitHub Desktop.
Save Siedrix/1492190 to your computer and use it in GitHub Desktop.
Web server for android
var WebSocket = require('faye-websocket'),
http = require('http'),
express = require('express');
var app = express.createServer()
app.get('/', function(req, res){
res.send('Hello World');
});
app.get('/messages/:message',function(req, res){
console.log('New message?', req.params.message);
res.send('Your message is:' + req.params.message);
})
app.addListener('upgrade', function(request, socket, head) {
var ws = new WebSocket(request, socket, head);
console.log('new websockets');
setInterval(function () {
ws.send('Do you hear me?');
},5000);
ws.onmessage = function(event) {
console.log('Message:',event.data)
ws.send(event.data);
};
ws.onclose = function(event) {
console.log('close', event.code, event.reason);
ws = null;
};
});
app.listen(8080);
console.log('Server running at 127.0.0.1:8080');
var WebSocket = require('faye-websocket'),
port = 8080,
scheme = 'ws',
ws = new WebSocket.Client(scheme + '://localhost:' + port + '/');
console.log('Connecting to ' + ws.url);
ws.onopen = function(event) {
console.log('open');
ws.send('Hello, WebSocket!');
};
ws.onmessage = function(event) {
console.log('message', event.data);
// ws.close(1002, 'Going away');
};
ws.onclose = function(event) {
console.log('close', event.code, event.reason);
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment