JavaScript Adobe AIR Socket Wrapper

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了JavaScript Adobe AIR Socket Wrapper相关的知识,希望对你有一定的参考价值。

/**
 *	Wrapper for AIR's network functions.
 *  @author Juntalis
 */

/**
 * Wrapper for the TCP Socket
 * Make sure to call socket.init() or else it wont work.
 */
function socket() {
	/**
	 * Whether or not our socket is initialized.
	 */
	var _blInit = false;
	/* Callbacks */
	var _cbConnHandle = null;
	var _cbCloseHandle = null;
	var _cbRecvHandle = null;
	var _cbIOErrHandle = null;
	var _cbSecHandle = null;
	
	/**
	 * Returns a socket's connection status.
	 * @return {Boolean}
	 */
	this.connected = function() {
		if (this.objSocket != null) {
			return this.objSocket.connected;
		} else {
			this.init();
			return false;
		}
	}
	
	/**
	 * Assign a handler for connection.
	 * @param {Function} cb	Needs to be function(event).
	 */
	this.handleConnection = function(cb) {
		if(cb) _cbConnHandle = cb;
	}
	
	/**
	 * Assign a handler for disconnection.
	 * @param {Function} cb	Needs to be function(event).
	 */
	this.handleClose = function(cb) {
		if(cb) _cbCloseHandle = cb;
	}
	
	/**
	 * Assign a handler for data receiving.
	 * @param {Function} cb	Needs to be function(event).
	 */
	this.handleReceives = function(cb) {
		if(cb) _cbRecvHandle = cb;
	}
	
	/**
	 * Assign a handler for IO errors.
	 * @param {Function} cb	Needs to be function(event).
	 */
	this.handleIOErrors = function(cb) {
		if(cb) _cbIOErrHandle = cb;
	}
	
	/**
	 * Assign a handler for security errors.
	 * @param {Function} cb	Needs to be function(event).
	 */
	this.handleSecErrors = function(cb) {
		if(cb) _cbSecErrHandle = cb;
	}
		
	/**
	 * The actual AIR socket being used.
	 */
	this.objSocket = null;
	
	/**
	 * Proxy to AIR socket's connect.
	 */
	this.connect;
	
	/**
	 * Proxy to AIR socket's close.
	 */
	this.close;
	
	/**
	 * Proxy to AIR socket's close.
	 */
	this.disconnect;
	
	/**
	 * Sends msg to the server. Automatically checks for a \n character at the end of the message. If none, it adds one.
	 * @param {Object} msg String to send to the server.
	 */
	this.send = function(msg) {
        if(!msg) return;
		if (msg.charAt(msg.length) != "\n") msg += "\n";
		
        try {
            this.objSocket.writeUTFBytes(msg);
			this.objSocket.flush();
        } catch(err) {
            if (DEBUG) air.trace(err);
        }
    };
	
	/**
	 * Receives all available bytes from our socket.
	 * @return {String}
	 */
	this.recv = function() {
		try {
			var strData = this.objSocket.readUTFBytes( this.objSocket.bytesAvailable );
		} catch(err) {
            if (DEBUG) air.trace(err);
        }
		return strData;
	};
	
	/**
	 * Socket Connection Handler
	 * @param {Object} event
	 */
	function _onConnect(event)
	{
		if (DEBUG) air.trace("ConnectHandler: " + event);
		if (_cbConnHandle) _cbConnHandle(event);
	};

	/**
	 * Socket Disconnection Handler
	 * @param {Object} event
	 */
	function _onClose(event)
	{
		if (DEBUG) air.trace("CloseHandler: " + event);
		if (_cbCloseHandle) _cbCloseHandle(event);
	};
		
	/**
	 * Receive Handler
	 * @param {Object} event
	 */
	function _onReceive(event)
	{
		 if (DEBUG) air.trace("ReceiveHandler: " + event);
		 if (_cbRecvHandle) _cbRecvHandle(event);
	}
	
	/**
	 * Input/Output Error Handler
	 * @param {Object} event
	 */
	function _onIOError(event)
	{
		if (DEBUG) air.trace("IOErrorHandler: " + event);
		if (_cbIOErrHandle) _cbIOErrHandle(event);
	}
	
	/**
	 * Security Error Handler
	 * @param {Object} event
	 */
	function _onSecurityError(event)
	{
		if (DEBUG) air.trace("SecurityErrorHandler: " + event);
		if (_cbSecErrHandle) _cbSecErrvHandle(event);
	}
		
	/**
	 * Initializes the socket.
	 */
	this.init = function()
	{
		if(_blInit) return;
		this.objSocket = new air.Socket();
		this.objSocket.addEventListener(air.Event.CLOSE, _onClose);
    	this.objSocket.addEventListener(air.Event.CONNECT, _onConnect);
    	this.objSocket.addEventListener(air.IOErrorEvent.IO_ERROR, _onIOError);
    	this.objSocket.addEventListener(air.SecurityErrorEvent.SECURITY_ERROR, _onSecurityError);
    	this.objSocket.addEventListener(air.ProgressEvent.SOCKET_DATA, _onReceive);
		this.connect = this.objSocket.connect;
		this.close = this.objSocket.close;
		this.disconnect = this.objSocket.close;
		_blInit = true;
	}
	
}

以上是关于JavaScript Adobe AIR Socket Wrapper的主要内容,如果未能解决你的问题,请参考以下文章

Adobe AIR SDK 和 Flex SDK 的关系?

Adobe air(HTML + JS)阻止图像,输入和文本区域的右键单击事件

Adobe Air Introspector命令

linux adobe air 无法安装

Adobe Air - 使用 Air 打开文件

Adobe Air .air文件:是32位,64位还是都不是?