Skip to content

Instantly share code, notes, and snippets.

@tzmartin
Last active August 29, 2015 14:09
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save tzmartin/a10a472815abce5e7aba to your computer and use it in GitHub Desktop.
Save tzmartin/a10a472815abce5e7aba to your computer and use it in GitHub Desktop.
Titanium WebView as a Websocket Processor
module.exports = function(_args) {
/*
This is a simple WebView running as a websocket client. Here are notes:
- To test, run a local socket server (ie, socket.io). Socket.io example chat app: http://socket.io/get-started/chat/
- Pass the websocket URL into io.connect() method
- Event callbacks are registered on Ti.App.. app should fire 'app:ws:connected'. Observe console.log: "Websocket is connected"
- For securty purposes, use inline HTML (string below) to compile into bytecode, rather than filesystem / HTML resources.
- The final webview has to be painted to the screen to instantiate Webkit. Simply 1x1 pixel with offset positiong. Webview becomes a processor.
*/
var html = "<html><head><script src='https://cdn.socket.io/socket.io-1.2.0.js' type='text/javascript'></script><script>function go(){var socket = io.connect('"+_args.url+"'); socket.on('connect', function (e) {Ti.App.fireEvent('app:ws:connected',e);}); };</script></head><body onload=\"go();\"></body></html>";
return Ti.UI.createWebView({
html:html,
width:1,
height:1,
left:-1,
top:-1,
opacity:0
});
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment