package net
{
import flash.events.Event;
import flash.events.EventDispatcher;
import flash.events.IOErrorEvent;
import flash.events.ProgressEvent;
import flash.events.SecurityErrorEvent;
import flash.net.Socket;
import flash.utils.ByteArray;
import flash.utils.Endian;
import org.aswing.JOptionPane;
import test.UserControlTest;
import ui.ConsolePane;
public class AppSocket extends EventDispatcher
{
protected var debug:Boolean = false;
protected var callerUrl:String;
protected var socketURL:String;
//vars returned from discovery
public var sessionID:String;
protected var heartBeatTimeout:int;
protected var connectionClosingTimeout:int;
protected var protocols:Array;
//hold over variables from constructor for discover to use
private var domain:String;
private var protocol:String;
private var proxyHost:String;
private var proxyPort:int;
private var headers:String;
private var ackRegexp:RegExp = new RegExp('(\\d+)\\+(.*)');
private var ackId:int = 0;
private var acks:Object = {};
public function AppSocket()
{
}
public function initSocket():void
{
socket = new Socket();
init();
addEvent();
}
public function setSocket(service:Socket):void
{
socket = service;
init();
addEvent();
}
private function init():void
{
socket.endian = Endian.BIG_ENDIAN;
socket.objectEncoding = 0;
// bean = new ResponseBeanParse();
cacheData = new ByteArray();
}
private function addEvent():void
{
socket.addEventListener(Event.CONNECT, onConnect);
socket.addEventListener(Event.CLOSE, onClose);
socket.addEventListener(ProgressEvent.SOCKET_DATA, onData);
socket.addEventListener(IOErrorEvent.IO_ERROR, onError);
socket.addEventListener(SecurityErrorEvent.SECURITY_ERROR, onSecurity);
}
public function connect(host:String, port:int):void
{
CONFIG::local
{
trace('connect:' + host + ':' + port);
}
this.host = host;
this.port = port;
socket.connect(host, port);
}
public function close():void
{
socket.close();
}
/**
* 发送数据
*
* @param cmd 命令代号
* @param data 数据
*/
public function sendData(req:String):void
{
CONFIG::local
{
trace("send:" + req.toString());
}
if (socket.connected)
{
//
var str:String = JSON.stringify(req);
//
trace(str);
var buffer:ByteArray = new ByteArray();
buffer.writeUTFBytes(req);
socket.writeShort(buffer.length);
socket.writeBytes(buffer);
socket.flush();
ConsolePane.ins.addClientMsg(req);
}
}
/**
* 连接了服务器时候响应
*
* @param e
*/
private function onConnect(e:Event):void
{
CONFIG::local
{
trace("socket has been connected!");
}
UserControlTest.getInstance().init();
this.dispatchEvent(e);
// var str:String = '2["connection",{}]';
// NetService.instance.sendData(str);
//
NetService.instance.sendData({action:'userjoin', session:'test', gameid:' fishing_haiwang2', tableid: 100});
}
/**
* 服务器主动断开socket 时候响应
*
* @param e
*/
private function onClose(e:Event):void
{
CONFIG::local
{
trace("socket has been closed!");
}
this.dispatchEvent(e);
}
/**
* 接收服务器数据响应事件
* @param e
*/
private function onData(e:ProgressEvent):void
{
var socketBytes:ByteArray = new ByteArray();
socket.readBytes(socketBytes, 0, socket.bytesAvailable);
socketBytes.position = 0;
check(socketBytes); // 数据处理
}
public function check(dataAry:ByteArray):void
{
cacheData.position = cacheData.length;
cacheData.writeBytes(dataAry);
cacheData.position = 0;
processCacheData();
}
public function processCacheData():void
{
if(cacheData.length >= 2)
{
var length:int = cacheData.readShort();
if(cacheData.length >= length + 2)
{
cacheData.position = 0;
var d:ByteArray = new ByteArray();
d.writeBytes(cacheData, 2, length);
d.position = 0;
processData(d);
cacheData.position = 0;
var l:ByteArray = new ByteArray();
l.writeBytes(cacheData, (length + 2));
cacheData = l;
cacheData.position = 0;
processCacheData();
}
}
}
private function processData(d:ByteArray):void
{
var str:String = d.readUTFBytes(d.bytesAvailable);
try{
var msg:Object = JSON.parse(str);
}catch(e:Error){
trace(e.message);
return;
}
ConsolePane.ins.addServerMsg(str);
trace('收到服务器消息:'+str);
if(msg.content.status == "error"){
JOptionPane.showMessageDialog("", msg.content.error + "\n" + msg.content.debug);
return;
}
var command:ICommad = ActionRegister.getAction(msg.event);
if(command) command.excute(msg);
// var responseBean:AbstractResponseBean = bean.parse(allData);
// if (responseBean)
// {
// var msgCode:int = responseBean.getMsgCode();
// var action:ICommad = ActionRegister.getAction(msgCode);
// if (action)
// {
// action.excute(responseBean);
// }
// else
// {
// CONFIG::local
// {
// trace("没处理的消息:" + msgCode);
// }
// }
// CONFIG::local
// {
//
trace("handler:" + responseBean.toString());
// }
// }
// else
// {
// CONFIG::local
// {
//
trace("没处理的消息");
// }
// }
}
private function onError(e:Event):void
{
CONFIG::local
{
trace("socket io error ? !");
}
this.dispatchEvent(e);
}
private function onSecurity(e:Event):void
{
CONFIG::local
{
trace("socket security error !");
}
this.dispatchEvent(e);
}
public function get isConnected():Boolean
{
return _isConnected;
}
public function set isConnected(value:Boolean):void
{
_isConnected = value;
}
private var headStr:String;
private var socket:Socket;
/**
* 服务器地址
*/
private var host:String;
/**
* 服务器端口
*/
private var port:int;
/**
* socket连接成功
*/
private var _isConnected:Boolean = false;
private var cacheData:ByteArray; //缓存的数据
}
}
==========================如果你觉得对你有用,请支持一下我,随便打赏点,谢谢======================