Skip to content

Commit

Permalink
add transport websocket: ti-websocket-client <https://github.com/masu…
Browse files Browse the repository at this point in the history
  • Loading branch information
Yusuke Hata committed Jan 19, 2012
1 parent c8cc79b commit 2868f91
Show file tree
Hide file tree
Showing 10 changed files with 3,293 additions and 7 deletions.
45 changes: 38 additions & 7 deletions Resources/socket.io-titanium.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,23 +13,53 @@ require('socket.io/lib/events');
require('socket.io/lib/namespace');
require('socket.io/lib/transport');

// T.B.D: websocket
io.transports = ['xhr-polling'];

require('socket.io/lib/transports/xhr');
io.transports.forEach(function(t){
require('socket.io/lib/transports/' + t);
});
require('socket.io/lib/transports/xhr-polling');
require('socket.io/lib/transports/websocket');

require('socket.io/lib/socket');

// {{{ @lib/transports/ws
// titanium-websocket-client :: https://github.com/masuidrive/ti-websocket-client
var WebSocket = require('ti-websocket-client/ti-websocket-client').WebSocket;
io.Transport.websocket.prototype.open = function (){
var query = io.util.query(this.socket.options.query)
, self = this
, Socket

var Socket = WebSocket;

this.websocket = new Socket(this.prepareUrl() + query);

this.websocket.onopen = function () {
self.onOpen();
self.socket.setBuffer(false);
};
this.websocket.onmessage = function (ev) {
self.onData(ev.data);
};
this.websocket.onclose = function () {
self.onClose();
self.socket.setBuffer(true);
};
this.websocket.onerror = function (e) {
self.onError(e);
};

return this;
};
// }}} @lib/transports/ws

// {{{ @lib/socket.js
io.Socket.prototype.isXDomain = function (){
return false;
};
// }}} @lib/socket.js

// {{{ @lib/util.js
io.util.request = function (){
return Titanium.Network.createHTTPClient();
};

io.connect = function (host, details) {
var uri = io.util.parseUri(host)
, uuri
Expand Down Expand Up @@ -66,6 +96,7 @@ io.connect = function (host, details) {
// if path is different from '' or /
return socket.of(uri.path.length > 1 ? uri.path : '');
};
// }}} @lib/util.js

exports = {
io: io,
Expand Down
47 changes: 47 additions & 0 deletions Resources/ti-websocket-client/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
# WebSocket client for Titanium Mobile

It's implementation of WebSocket client for Titanium Mobile.


## Supported protocols

* [hybi-07](http://tools.ietf.org/html/draft-ietf-hybi-thewebsocketprotocol-07)


## How to use it

copy "ti-websocket-client.js" to your app Resources directory.

```javascript
var WebSocket = require('ti-websocket-client').WebSocket;

ws = new WebSocket("ws://localhost:3000/");

ws.onopen = function () {
alert("Connected");
ws.send("Hello");
};

ws.onclose = function () {
alert("Disconnected");
};

ws.onmessage = function (message) {
alert("> "+message.data);
};

ws.onerror = function (e) {
alert('Error: ' + (e ? JSON.stringify(e) : 'A unknown error occurred'));
};
```

## Progress

* Working good with node and ruby's websocket servers
* You can run on Android and iPhone.


## License

MIT License.
Copyright 2011 Yuichiro MASUI <masui@masuidrive.jp>
29 changes: 29 additions & 0 deletions Resources/ti-websocket-client/bin/builder.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
/**
* Titanium WebSocket Client
*
* http://github.com/masuidrive/ti-websocket-client
*
* Copyright 2011 Yuichiro MASUI <masui@masuidrive.jp>
* MIT License
*/

var LIB_DIR = __dirname + '/../lib/';

var fs = require('fs');

var extract_require = function(filename) {
var content = fs.readFileSync(LIB_DIR + filename + '.js', 'utf8');
return content.replace(/require\s*\(\s*['"]{1}(.*?)['"]{1}\s*\)/g, function(full, fname) {
var exports = extract_require(fname);
return "(function(){var exports={};" + exports + "return exports;}())"
});
};

fs.write(
fs.openSync(__dirname + '/../ti-websocket-client.js', 'w')
, extract_require('websocket-client')
, 0
, 'utf8'
);

console.log('Successfully generated the build: ti-websocket-client.js');
108 changes: 108 additions & 0 deletions Resources/ti-websocket-client/examples/echo-client.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,108 @@
//
// WebSocket client sample app for Titanium Mobile
//
// Yuichiro MASUI <masui@masuidrive.jp>
// MIT License
var WebSocket = require('ti-websocket-client').WebSocket;

var win = Titanium.UI.createWindow({
title:'WebSocket',
backgroundColor:'#fff'
});

var textarea = Titanium.UI.createTextArea({
backgroundColor: "#eee",
value: '',
editable: false,
top: 70,
left: 0,
right: 0,
bottom: 0
});
win.add(textarea);

var connectBtn = Titanium.UI.createButton({
title:'Connect',
font:{fontSize:16,fontFamily:'Helvetica Neue'},
textAlign:'center',
width: 100,
height: 20,
top: 5,
left: 5
});
win.add(connectBtn);

var ws;
connectBtn.addEventListener('click', function() {
ws = new WebSocket("ws://localhost:3000/");

ws.onopen = function () {
Ti.API.info("message Connected");
log("Connected");
};

ws.onclose = function () {
log("Disconnected");
};

ws.onmessage = function (message) {
log("> "+message.data);
};

ws.onerror = function (e) {
log('Error: ' + (e ? JSON.stringify(e) : 'A unknown error occurred'));
};

log("Connecting...");
});

var closeBtn = Titanium.UI.createButton({
title:'Close',
font:{fontSize:16,fontFamily:'Helvetica Neue'},
textAlign:'center',
width:100,
height: 20,
top: 5,
right: 5
});

win.add(closeBtn);
closeBtn.addEventListener('click', function() {
ws.close();
});

var log = function(str) {
textarea.value += str + "\n";
};

var messageField = Ti.UI.createTextField({
borderStyle:Titanium.UI.INPUT_BORDERSTYLE_ROUNDED,
width:230,
height: 30,
top: 35,
left: 5
});
win.add(messageField);

var sendBtn = Titanium.UI.createButton({
title:'Send',
font:{fontSize:16,fontFamily:'Helvetica Neue'},
textAlign:'center',
width:70,
height: 30,
top: 35,
right: 5
});
win.add(sendBtn);
sendBtn.addEventListener('click', function() {
var v = messageField.value;
log('< ' + v);
ws.send(v);
messageField.value = "";
messageField.blur();
});



win.open();

Loading

0 comments on commit 2868f91

Please sign in to comment.