Skip to content

Instantly share code, notes, and snippets.

@shouse
Last active August 29, 2015 14:17
Show Gist options
  • Save shouse/dfb4612a8d77a441fad8 to your computer and use it in GitHub Desktop.
Save shouse/dfb4612a8d77a441fad8 to your computer and use it in GitHub Desktop.
Titanium WebSocket demo. Based on https://github.com/omorandi/tiws
function websocketSetup() {
//@TODO check config.json for socket setting
uri = 'ws://localhost:8765';
tiws = require('net.iamyellow.tiws').createWS();
tiws.addEventListener('open', function () {
alert('opened')
_self.info('Websocket opened');
socketConnected = true;
websocketSendMessage("THIS IS A TEST MESSAGE FROM TITANIUM");
});
tiws.addEventListener('close', function (ev) {
Ti.API.info(ev);
socketConnected = false;
});
tiws.addEventListener('error', function (ev) {
Ti.API.error(ev);
});
tiws.addEventListener('message', function (ev) {
Ti.API.log(ev);
alert('WS Message: ' + JSON.stringify(ev, null, 4));
});
tiws.open(uri);
}
function websocketSendMessage(message) {
tiws.send(message);
}
console.log('Starting WebSocketServer!');
var WebSocketServer = require('ws').Server
, wss = new WebSocketServer({port: 8765});
wss.on('connection', function(ws) {
console.log('CONNECTED TO WS!');
ws.on('message', function(message) {
console.log('received: %s', message);
});
ws.send('something');
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment