Adobe AIR Socket Wrapper


/ Published in: JavaScript
Save to your folder(s)

<u>Example usage:</u>

<pre>
var sckLobby = new socket();
var strBuffer = "";
sckLobby.init();
sckLobby.handleReceives(
function(event) {
strBuffer += sckLobby.recv();
sckLobby.send("Hello World!");
}
);
sckLobby.handleClose(
function(event) {
if(!strBuffer) return;
document.write(strBuffer);
}
);
sckLobby.connect('127.0.0.1', 8000);
</pre>


Copy this code and paste it in your HTML
  1. /**
  2.  * Wrapper for AIR's network functions.
  3.  * @author Juntalis
  4.  */
  5.  
  6. /**
  7.  * Wrapper for the TCP Socket
  8.  * Make sure to call socket.init() or else it wont work.
  9.  */
  10. function socket() {
  11. /**
  12. * Whether or not our socket is initialized.
  13. */
  14. var _blInit = false;
  15. /* Callbacks */
  16. var _cbConnHandle = null;
  17. var _cbCloseHandle = null;
  18. var _cbRecvHandle = null;
  19. var _cbIOErrHandle = null;
  20. var _cbSecHandle = null;
  21.  
  22. /**
  23. * Returns a socket's connection status.
  24. * @return {Boolean}
  25. */
  26. this.connected = function() {
  27. if (this.objSocket != null) {
  28. return this.objSocket.connected;
  29. } else {
  30. this.init();
  31. return false;
  32. }
  33. }
  34.  
  35. /**
  36. * Assign a handler for connection.
  37. * @param {Function} cb Needs to be function(event).
  38. */
  39. this.handleConnection = function(cb) {
  40. if(cb) _cbConnHandle = cb;
  41. }
  42.  
  43. /**
  44. * Assign a handler for disconnection.
  45. * @param {Function} cb Needs to be function(event).
  46. */
  47. this.handleClose = function(cb) {
  48. if(cb) _cbCloseHandle = cb;
  49. }
  50.  
  51. /**
  52. * Assign a handler for data receiving.
  53. * @param {Function} cb Needs to be function(event).
  54. */
  55. this.handleReceives = function(cb) {
  56. if(cb) _cbRecvHandle = cb;
  57. }
  58.  
  59. /**
  60. * Assign a handler for IO errors.
  61. * @param {Function} cb Needs to be function(event).
  62. */
  63. this.handleIOErrors = function(cb) {
  64. if(cb) _cbIOErrHandle = cb;
  65. }
  66.  
  67. /**
  68. * Assign a handler for security errors.
  69. * @param {Function} cb Needs to be function(event).
  70. */
  71. this.handleSecErrors = function(cb) {
  72. if(cb) _cbSecErrHandle = cb;
  73. }
  74.  
  75. /**
  76. * The actual AIR socket being used.
  77. */
  78. this.objSocket = null;
  79.  
  80. /**
  81. * Proxy to AIR socket's connect.
  82. */
  83. this.connect;
  84.  
  85. /**
  86. * Proxy to AIR socket's close.
  87. */
  88. this.close;
  89.  
  90. /**
  91. * Proxy to AIR socket's close.
  92. */
  93. this.disconnect;
  94.  
  95. /**
  96. * Sends msg to the server. Automatically checks for a \n character at the end of the message. If none, it adds one.
  97. * @param {Object} msg String to send to the server.
  98. */
  99. this.send = function(msg) {
  100. if(!msg) return;
  101. if (msg.charAt(msg.length) != "\n") msg += "\n";
  102.  
  103. try {
  104. this.objSocket.writeUTFBytes(msg);
  105. this.objSocket.flush();
  106. } catch(err) {
  107. if (DEBUG) air.trace(err);
  108. }
  109. };
  110.  
  111. /**
  112. * Receives all available bytes from our socket.
  113. * @return {String}
  114. */
  115. this.recv = function() {
  116. try {
  117. var strData = this.objSocket.readUTFBytes( this.objSocket.bytesAvailable );
  118. } catch(err) {
  119. if (DEBUG) air.trace(err);
  120. }
  121. return strData;
  122. };
  123.  
  124. /**
  125. * Socket Connection Handler
  126. * @param {Object} event
  127. */
  128. function _onConnect(event)
  129. {
  130. if (DEBUG) air.trace("ConnectHandler: " + event);
  131. if (_cbConnHandle) _cbConnHandle(event);
  132. };
  133.  
  134. /**
  135. * Socket Disconnection Handler
  136. * @param {Object} event
  137. */
  138. function _onClose(event)
  139. {
  140. if (DEBUG) air.trace("CloseHandler: " + event);
  141. if (_cbCloseHandle) _cbCloseHandle(event);
  142. };
  143.  
  144. /**
  145. * Receive Handler
  146. * @param {Object} event
  147. */
  148. function _onReceive(event)
  149. {
  150. if (DEBUG) air.trace("ReceiveHandler: " + event);
  151. if (_cbRecvHandle) _cbRecvHandle(event);
  152. }
  153.  
  154. /**
  155. * Input/Output Error Handler
  156. * @param {Object} event
  157. */
  158. function _onIOError(event)
  159. {
  160. if (DEBUG) air.trace("IOErrorHandler: " + event);
  161. if (_cbIOErrHandle) _cbIOErrHandle(event);
  162. }
  163.  
  164. /**
  165. * Security Error Handler
  166. * @param {Object} event
  167. */
  168. function _onSecurityError(event)
  169. {
  170. if (DEBUG) air.trace("SecurityErrorHandler: " + event);
  171. if (_cbSecErrHandle) _cbSecErrvHandle(event);
  172. }
  173.  
  174. /**
  175. * Initializes the socket.
  176. */
  177. this.init = function()
  178. {
  179. if(_blInit) return;
  180. this.objSocket = new air.Socket();
  181. this.objSocket.addEventListener(air.Event.CLOSE, _onClose);
  182. this.objSocket.addEventListener(air.Event.CONNECT, _onConnect);
  183. this.objSocket.addEventListener(air.IOErrorEvent.IO_ERROR, _onIOError);
  184. this.objSocket.addEventListener(air.SecurityErrorEvent.SECURITY_ERROR, _onSecurityError);
  185. this.objSocket.addEventListener(air.ProgressEvent.SOCKET_DATA, _onReceive);
  186. this.connect = this.objSocket.connect;
  187. this.close = this.objSocket.close;
  188. this.disconnect = this.objSocket.close;
  189. _blInit = true;
  190. }
  191.  
  192. }

Report this snippet


Comments

RSS Icon Subscribe to comments

You need to login to post a comment.