Created
May 16, 2012 13:23
-
-
Save laclefyoshi/2710304 to your computer and use it in GitHub Desktop.
sample code for using socket API in Google Chrome
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
var udp_socket_writer = function(host, port, string) { | |
var string2ArrayBufferAndWrite = function(str) { // http://goo.gl/1eJnn | |
var bb = new WebKitBlobBuilder(); | |
bb.append(str); | |
var f = new FileReader(); | |
f.onload = function(e) { | |
var data = e.target.result; | |
chrome.experimental.socket.create( | |
'udp', {}, | |
function(socketInfo) { | |
chrome.experimental.socket.connect( | |
socketInfo.socketId, host, port, | |
function(result) { | |
chrome.experimental.socket.write(socketInfo.socketId, data, | |
function(writeInfo){ console.log(writeInfo); }) | |
}); | |
}); | |
}; | |
f.readAsArrayBuffer(bb.getBlob()); | |
}; | |
string2ArrayBufferAndWrite(string); | |
} | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment