-
Notifications
You must be signed in to change notification settings - Fork 34
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
add transport websocket: ti-websocket-client <https://github.com/masu…
- Loading branch information
Yusuke Hata
committed
Jan 19, 2012
1 parent
c8cc79b
commit 2868f91
Showing
10 changed files
with
3,293 additions
and
7 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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'); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(); | ||
|
Oops, something went wrong.