AIR android serversocket 仅适用于 ADL(空中调试启动器)
AIR android serversocket works only in ADL (air debug launcher)
我的问题是我正在开发一个通过 serversocket(服务器)套接字(客户端)相互通信的 air 应用程序
当我在 Air 调试启动器中测试它时,服务器端一切正常(我可以从不同的互联网连接看到服务器)
但是当我尝试从 Android 设备托管时,它只能在 LAN 上运行(无法从不同的连接看到托管服务器)
我的问题是为什么它在测试环境中有效,但在构建的 APP 中却无效。真令人沮丧。
这是我的服务器端代码:
package AS {
import flash.display.MovieClip;
import AS.Constants;
import flash.net.ServerSocket;
import flash.net.Socket;
import flash.events.ServerSocketConnectEvent;
import flash.events.ProgressEvent;
import flash.events.Event;
import flash.events.MouseEvent;
import flash.events.KeyboardEvent;
import flash.text.TextFieldType;
import flash.net.SharedObject;
import fl.data.DataProvider;
import flash.net.URLRequest;
import flash.media.Sound;
import flash.desktop.NativeApplication;
import flash.desktop.SystemIdleMode;
import flash.text.TextFormat;
import flash.net.URLLoader;
import flash.net.URLRequest;
import flash.net.URLRequestMethod;
public class chatPage extends MovieClip {
private var server:ServerSocket;
private var clients:Vector.<Socket> = new Vector.<Socket>;
private var clientUsernames:Array = new Array();
private var autoScroll:Boolean=true;
private var autoClean:Boolean=false;
private var counter:int=900;
private var soundwarn:Boolean=true;
private var myformat:TextFormat=new TextFormat();
public function chatPage(address:String="0.0.0.0",port:uint=8888):void {
server = new ServerSocket();
server.bind(port,address);
server.addEventListener(ServerSocketConnectEvent.CONNECT, serverConnectHandler);
server.listen();
if(!server.listening){
die();
Constants.mainRef.initLoginScreen("not listening");
}else if(!server.bound){
die();
Constants.mainRef.initLoginScreen("not bound");
}
myformat.color=0xffffff;
client_list.setRendererStyle("textFormat",myformat);
soundButton.gotoAndStop(1);
chat_txt.text = String("");
chat_txt.scrollV=chat_txt.maxScrollV;
scrollBar.update();
scrollBar.addEventListener(Event.SCROLL, scrollingListener);
send_txt.addEventListener(KeyboardEvent.KEY_DOWN, commandHandler);
autoClean_btn.addEventListener(Event.CHANGE, autoCleanHandler);
soundButton.addEventListener(MouseEvent.CLICK, soundClickHandler);
close_btn.addEventListener(MouseEvent.CLICK, closeHandler);
kick_btn.addEventListener(MouseEvent.CLICK, kickHandler);
ban_btn.addEventListener(MouseEvent.CLICK, banHandler);
burn_btn.addEventListener(MouseEvent.CLICK, burnHandler);
share_btn.addEventListener(MouseEvent.CLICK, shareHandler);
scroll_down_btn.addEventListener(MouseEvent.CLICK, scrollDownHandler);
preventSleep_check.addEventListener(Event.CHANGE, sleepChangeHandler);
}
private function burnHandler(e:MouseEvent):void {
//burn all evidence for you and all your clients
if(chat_txt.length > 0){
send(4,Constants.mainRef.encrypt("burn"));
chat_txt.text = String("");
chat_txt.scrollV=chat_txt.maxScrollV;
scrollBar.update();
}
}
private function shareHandler(e:MouseEvent):void {
//share the contents of your chat_txt to all your clients
if(chat_txt.length > 0){
send(5,Constants.mainRef.encrypt(chat_txt.text));
}
}
private function banHandler(e:MouseEvent):void {
var tempIpArr:Array = new Array();
for (var i: uint = 0; i < clients.length; i++) {
tempIpArr.push(clients[i].remoteAddress);
}
Constants.STAGE.addChild(new listDialog(clientUsernames, tempIpArr, this) );
}
private function sleepChangeHandler(e:Event):void{
if(this.preventSleep_check.selected){
NativeApplication.nativeApplication.systemIdleMode = SystemIdleMode.KEEP_AWAKE;
}else{
NativeApplication.nativeApplication.systemIdleMode = SystemIdleMode.NORMAL;
}
}
private function kickHandler(e:MouseEvent):void {
if(client_list.selectedIndex > -1){
clients[client_list.selectedIndex].close();
clients[client_list.selectedIndex].removeEventListener(ProgressEvent.SOCKET_DATA, socketDataHandler);
clients[client_list.selectedIndex].removeEventListener(Event.CLOSE, socketDisconnectHandler);
clients.splice(client_list.selectedIndex,1);
//send("▼"+clientUsernames[client_list.selectedIndex]);
send(3,String(clientUsernames[client_list.selectedIndex]));
clientUsernames.splice(client_list.selectedIndex,1);
client_list.dataProvider = new DataProvider( clientUsernames );
}
}
public function remoteKick(index:uint):void{
clients[index].close();
clients[index].removeEventListener(ProgressEvent.SOCKET_DATA, socketDataHandler);
clients[index].removeEventListener(Event.CLOSE, socketDisconnectHandler);
clients.splice(index,1);
send(3,String(clientUsernames[index]));
clientUsernames.splice(index,1);
client_list.dataProvider = new DataProvider( clientUsernames );
}
private function scrollDownHandler(e:MouseEvent):void {
autoScroll=true;
chat_txt.scrollV=chat_txt.maxScrollV;
scrollBar.update();
}
private function closeHandler(e:MouseEvent):void {
die();
Constants.mainRef.initLoginScreen("Chat room terminated!");
//Constants.STAGE.addChild(new logInScreen("Chat room terminated!"));
}
private function soundClickHandler(e:MouseEvent):void {
if(soundButton.currentFrame == 1){
soundwarn=false;
soundButton.gotoAndStop(2);
}else{
soundwarn=true;
soundButton.gotoAndStop(1);
}
}
private function autoCleanFrameListener(e:Event):void {
if(autoClean){
counter--;
counter_txt.text = String( Math.floor( counter / 30 ) + "s" );
if(counter <= 0){
counter_txt.text = String("30s");
chat_txt.text = String("");
chat_txt.scrollV=chat_txt.maxScrollV;
scrollBar.update();
currentDataStr = String("");
counter=900;
}
}
}
private function autoCleanHandler(e:Event):void {
if(autoClean_btn.selected){
autoClean=true;
counter_txt.text = String("30s");
this.addEventListener(Event.ENTER_FRAME, autoCleanFrameListener);
}else{
this.removeEventListener(Event.ENTER_FRAME, autoCleanFrameListener);
autoClean=false;
counter_txt.text = String("");
counter=900;
}
}
private function scrollingListener(e:Event):void {
if(chat_txt.scrollV!=chat_txt.maxScrollV){
autoScroll=false;
}
}
private function serverConnectHandler(e:ServerSocketConnectEvent):void {
var socket:Socket = e.socket;
var banned:Boolean;
if(Constants.banList.length > 0){
for (var i: uint = 0; i < Constants.banList.length; i++) {
if(Constants.banList[i] == socket.remoteAddress){
banned=true;
break;
}else{
banned=false;
}
}
}else{
banned=false;
}
if(!banned){
clients.push(socket);
clients[clients.length-1].addEventListener(ProgressEvent.SOCKET_DATA, socketDataHandler);
clients[clients.length-1].addEventListener(Event.CLOSE, socketDisconnectHandler);
send(2,Constants.mainRef.encrypt(Constants.mainRef.username));
}else{
socket.close();
}
}
private function socketDisconnectHandler(e:Event):void {
send(3,Constants.mainRef.encrypt( clientUsernames[clients.indexOf(e.target)] ) );
clientUsernames.splice(clients.indexOf(e.target),1);
client_list.dataProvider = new DataProvider( clientUsernames );
clients.splice(clients.indexOf(e.target),1);
e.target.close();
e.target.removeEventListener(ProgressEvent.SOCKET_DATA, socketDataHandler);
e.target.removeEventListener(Event.CLOSE, socketDisconnectHandler);
}
private var currentDataStr:String;
private var currentDataType:uint;
private function socketDataHandler(e:ProgressEvent):void {
var socket:Socket = e.currentTarget as Socket;
currentDataType = socket.readUnsignedInt();
currentDataStr = socket.readUTFBytes(socket.bytesAvailable);
send(currentDataType, currentDataStr);
if(currentDataType == 1){
chat_txt.appendText(Constants.mainRef.decrypt(currentDataStr) );
if(autoScroll){
chat_txt.scrollV=chat_txt.maxScrollV;
}
scrollBar.update();
if(soundwarn){
//give sound warning
var snd:Sound = new Sound(new URLRequest("sound/ding.mp3"));
snd.play();
}
}else if(currentDataType == 2){
var tempString:String = Constants.mainRef.decrypt(currentDataStr);
clientUsernames.push(tempString);
client_list.dataProvider = new DataProvider( clientUsernames );
}
tempString=String("");
currentDataStr=String("");
currentDataType=0;
}
private function commandHandler(e:KeyboardEvent):void {
if(e.keyCode==13){
//ENTER GOT PRESSED
var commandStr:String = send_txt.text;
if(commandStr.indexOf(".") == 0){
if(commandStr.indexOf(".del") > -1 ){
chat_txt.text = String("");
currentDataStr = String("");
chat_txt.scrollV=chat_txt.maxScrollV;
scrollBar.update();
}else if(commandStr.indexOf(".burn") > -1 ){
if(chat_txt.length > 0){
send(4,Constants.mainRef.encrypt("burn"));
chat_txt.text = String("");
chat_txt.scrollV=chat_txt.maxScrollV;
scrollBar.update();
}
}else if(commandStr.indexOf(".share") > -1 ){
if(chat_txt.length > 0){
send(5,Constants.mainRef.encrypt(chat_txt.text));
}
}else if(commandStr.indexOf(".ip") > -1 ){
getIpOnline();
}else if(commandStr.indexOf(".roll") > -1 ){
var rngMax:int = parseInt( commandStr.slice(commandStr.indexOf(" ")) );
if(rngMax > 0){
var rawString:String = Constants.mainRef.username + " rolled " + String(Constants.mainRef.randomNum(1,rngMax)) + " out of " + String(rngMax) + "\n";
send(1, Constants.mainRef.encrypt( rawString ) );
chat_txt.appendText( rawString );
if(autoScroll){
chat_txt.scrollV=chat_txt.maxScrollV;
}
scrollBar.update();
}
}else if(commandStr.indexOf(".kick") > -1 && client_list.selectedIndex > -1){
clients[client_list.selectedIndex].close();
clients[client_list.selectedIndex].removeEventListener(ProgressEvent.SOCKET_DATA, socketDataHandler);
clients[client_list.selectedIndex].removeEventListener(Event.CLOSE, socketDisconnectHandler);
clients.splice(client_list.selectedIndex,1);
send(3,Constants.mainRef.encrypt( clientUsernames[client_list.selectedIndex] ) );
clientUsernames.splice(client_list.selectedIndex,1);
client_list.dataProvider = new DataProvider( clientUsernames );
}
}else if(commandStr.length > 0){
send(1, Constants.mainRef.encrypt(Constants.mainRef.username + ": " + commandStr + "\n"));
chat_txt.appendText(Constants.mainRef.username + ": " + commandStr + "\n");
if(autoScroll){
chat_txt.scrollV=chat_txt.maxScrollV;
}
scrollBar.update();
}
send_txt.text = String("");
commandStr = String("");
Constants.STAGE.focus = null;
}
}
public function send(typeIndex:uint, data:String):void {
for each(var client:Socket in clients){
client.writeUnsignedInt(typeIndex);
client.writeUTFBytes(data);
client.flush();
}
}
private function getIpOnline():void{
var phpFileRequest:URLRequest = new URLRequest("https://api.ipify.org/");
phpFileRequest.method = URLRequestMethod.GET;
var phpLoader:URLLoader = new URLLoader();
phpLoader.addEventListener(Event.COMPLETE, showResult);
phpLoader.load(phpFileRequest);
}
private function showResult(e:Event):void{
send_txt.text = String(e.target.data);
e.target.removeEventListener(Event.COMPLETE, showResult);
}
public function die():void{
var i:uint
for(i = 0; i < clients.length; i++) {
clients[i].close();
clients[i].removeEventListener(ProgressEvent.SOCKET_DATA, socketDataHandler);
clients[i].removeEventListener(Event.CLOSE, socketDisconnectHandler);
}
server.removeEventListener(ServerSocketConnectEvent.CONNECT, serverConnectHandler);
server.close();
chat_txt.text = String("");
send_txt.text = String("");
kick_btn.removeEventListener(MouseEvent.CLICK, kickHandler);
ban_btn.removeEventListener(MouseEvent.CLICK, banHandler);
preventSleep_check.removeEventListener(Event.CHANGE, sleepChangeHandler);
close_btn.removeEventListener(MouseEvent.CLICK, closeHandler);
burn_btn.removeEventListener(MouseEvent.CLICK, burnHandler);
share_btn.removeEventListener(MouseEvent.CLICK, shareHandler);
scroll_down_btn.removeEventListener(MouseEvent.CLICK, scrollDownHandler);
soundButton.removeEventListener(MouseEvent.CLICK, soundClickHandler);
autoClean_btn.removeEventListener(Event.CHANGE, autoCleanHandler);
this.removeEventListener(Event.ENTER_FRAME, autoCleanFrameListener);
scrollBar.removeEventListener(Event.SCROLL, scrollingListener);
send_txt.removeEventListener(KeyboardEvent.KEY_DOWN, commandHandler);
clients.length=0;
clientUsernames.length=0;
if(Constants.cardIndexer.length > 0){
for (i = 0; i < Constants.cardIndexer.length; i++) {
Constants.cardIndexer[i].die(4);
}
Constants.cardIndexer.length=0;
Constants.deckRef.length=0;
}
this.parent.removeChild(this);
}
private function chatText(texty:String,chat:Boolean=true):void{
if(chat){
send(1, Constants.mainRef.encrypt(Constants.mainRef.username + ": " + texty + "\n"));
chat_txt.appendText(Constants.mainRef.username + ": " + texty + "\n");
if(autoScroll){
chat_txt.scrollV=chat_txt.maxScrollV;
}
scrollBar.update();
}else{
send(1, Constants.mainRef.encrypt(texty + "\n"));
chat_txt.appendText(texty + "\n");
if(autoScroll){
chat_txt.scrollV=chat_txt.maxScrollV;
}
scrollBar.update();
}
}
}
}
对于那些将来会遇到这个问题的人。
成功托管套接字服务器所需的主要是两件事
互联网:
-端口转发。
-将您的 air 应用程序放入防火墙 "allow" 列表中。
(在我的情况下,这是缺失的部分。哎哟!)
现在一切正常。
我的问题是我正在开发一个通过 serversocket(服务器)套接字(客户端)相互通信的 air 应用程序
当我在 Air 调试启动器中测试它时,服务器端一切正常(我可以从不同的互联网连接看到服务器)
但是当我尝试从 Android 设备托管时,它只能在 LAN 上运行(无法从不同的连接看到托管服务器)
我的问题是为什么它在测试环境中有效,但在构建的 APP 中却无效。真令人沮丧。
这是我的服务器端代码:
package AS {
import flash.display.MovieClip;
import AS.Constants;
import flash.net.ServerSocket;
import flash.net.Socket;
import flash.events.ServerSocketConnectEvent;
import flash.events.ProgressEvent;
import flash.events.Event;
import flash.events.MouseEvent;
import flash.events.KeyboardEvent;
import flash.text.TextFieldType;
import flash.net.SharedObject;
import fl.data.DataProvider;
import flash.net.URLRequest;
import flash.media.Sound;
import flash.desktop.NativeApplication;
import flash.desktop.SystemIdleMode;
import flash.text.TextFormat;
import flash.net.URLLoader;
import flash.net.URLRequest;
import flash.net.URLRequestMethod;
public class chatPage extends MovieClip {
private var server:ServerSocket;
private var clients:Vector.<Socket> = new Vector.<Socket>;
private var clientUsernames:Array = new Array();
private var autoScroll:Boolean=true;
private var autoClean:Boolean=false;
private var counter:int=900;
private var soundwarn:Boolean=true;
private var myformat:TextFormat=new TextFormat();
public function chatPage(address:String="0.0.0.0",port:uint=8888):void {
server = new ServerSocket();
server.bind(port,address);
server.addEventListener(ServerSocketConnectEvent.CONNECT, serverConnectHandler);
server.listen();
if(!server.listening){
die();
Constants.mainRef.initLoginScreen("not listening");
}else if(!server.bound){
die();
Constants.mainRef.initLoginScreen("not bound");
}
myformat.color=0xffffff;
client_list.setRendererStyle("textFormat",myformat);
soundButton.gotoAndStop(1);
chat_txt.text = String("");
chat_txt.scrollV=chat_txt.maxScrollV;
scrollBar.update();
scrollBar.addEventListener(Event.SCROLL, scrollingListener);
send_txt.addEventListener(KeyboardEvent.KEY_DOWN, commandHandler);
autoClean_btn.addEventListener(Event.CHANGE, autoCleanHandler);
soundButton.addEventListener(MouseEvent.CLICK, soundClickHandler);
close_btn.addEventListener(MouseEvent.CLICK, closeHandler);
kick_btn.addEventListener(MouseEvent.CLICK, kickHandler);
ban_btn.addEventListener(MouseEvent.CLICK, banHandler);
burn_btn.addEventListener(MouseEvent.CLICK, burnHandler);
share_btn.addEventListener(MouseEvent.CLICK, shareHandler);
scroll_down_btn.addEventListener(MouseEvent.CLICK, scrollDownHandler);
preventSleep_check.addEventListener(Event.CHANGE, sleepChangeHandler);
}
private function burnHandler(e:MouseEvent):void {
//burn all evidence for you and all your clients
if(chat_txt.length > 0){
send(4,Constants.mainRef.encrypt("burn"));
chat_txt.text = String("");
chat_txt.scrollV=chat_txt.maxScrollV;
scrollBar.update();
}
}
private function shareHandler(e:MouseEvent):void {
//share the contents of your chat_txt to all your clients
if(chat_txt.length > 0){
send(5,Constants.mainRef.encrypt(chat_txt.text));
}
}
private function banHandler(e:MouseEvent):void {
var tempIpArr:Array = new Array();
for (var i: uint = 0; i < clients.length; i++) {
tempIpArr.push(clients[i].remoteAddress);
}
Constants.STAGE.addChild(new listDialog(clientUsernames, tempIpArr, this) );
}
private function sleepChangeHandler(e:Event):void{
if(this.preventSleep_check.selected){
NativeApplication.nativeApplication.systemIdleMode = SystemIdleMode.KEEP_AWAKE;
}else{
NativeApplication.nativeApplication.systemIdleMode = SystemIdleMode.NORMAL;
}
}
private function kickHandler(e:MouseEvent):void {
if(client_list.selectedIndex > -1){
clients[client_list.selectedIndex].close();
clients[client_list.selectedIndex].removeEventListener(ProgressEvent.SOCKET_DATA, socketDataHandler);
clients[client_list.selectedIndex].removeEventListener(Event.CLOSE, socketDisconnectHandler);
clients.splice(client_list.selectedIndex,1);
//send("▼"+clientUsernames[client_list.selectedIndex]);
send(3,String(clientUsernames[client_list.selectedIndex]));
clientUsernames.splice(client_list.selectedIndex,1);
client_list.dataProvider = new DataProvider( clientUsernames );
}
}
public function remoteKick(index:uint):void{
clients[index].close();
clients[index].removeEventListener(ProgressEvent.SOCKET_DATA, socketDataHandler);
clients[index].removeEventListener(Event.CLOSE, socketDisconnectHandler);
clients.splice(index,1);
send(3,String(clientUsernames[index]));
clientUsernames.splice(index,1);
client_list.dataProvider = new DataProvider( clientUsernames );
}
private function scrollDownHandler(e:MouseEvent):void {
autoScroll=true;
chat_txt.scrollV=chat_txt.maxScrollV;
scrollBar.update();
}
private function closeHandler(e:MouseEvent):void {
die();
Constants.mainRef.initLoginScreen("Chat room terminated!");
//Constants.STAGE.addChild(new logInScreen("Chat room terminated!"));
}
private function soundClickHandler(e:MouseEvent):void {
if(soundButton.currentFrame == 1){
soundwarn=false;
soundButton.gotoAndStop(2);
}else{
soundwarn=true;
soundButton.gotoAndStop(1);
}
}
private function autoCleanFrameListener(e:Event):void {
if(autoClean){
counter--;
counter_txt.text = String( Math.floor( counter / 30 ) + "s" );
if(counter <= 0){
counter_txt.text = String("30s");
chat_txt.text = String("");
chat_txt.scrollV=chat_txt.maxScrollV;
scrollBar.update();
currentDataStr = String("");
counter=900;
}
}
}
private function autoCleanHandler(e:Event):void {
if(autoClean_btn.selected){
autoClean=true;
counter_txt.text = String("30s");
this.addEventListener(Event.ENTER_FRAME, autoCleanFrameListener);
}else{
this.removeEventListener(Event.ENTER_FRAME, autoCleanFrameListener);
autoClean=false;
counter_txt.text = String("");
counter=900;
}
}
private function scrollingListener(e:Event):void {
if(chat_txt.scrollV!=chat_txt.maxScrollV){
autoScroll=false;
}
}
private function serverConnectHandler(e:ServerSocketConnectEvent):void {
var socket:Socket = e.socket;
var banned:Boolean;
if(Constants.banList.length > 0){
for (var i: uint = 0; i < Constants.banList.length; i++) {
if(Constants.banList[i] == socket.remoteAddress){
banned=true;
break;
}else{
banned=false;
}
}
}else{
banned=false;
}
if(!banned){
clients.push(socket);
clients[clients.length-1].addEventListener(ProgressEvent.SOCKET_DATA, socketDataHandler);
clients[clients.length-1].addEventListener(Event.CLOSE, socketDisconnectHandler);
send(2,Constants.mainRef.encrypt(Constants.mainRef.username));
}else{
socket.close();
}
}
private function socketDisconnectHandler(e:Event):void {
send(3,Constants.mainRef.encrypt( clientUsernames[clients.indexOf(e.target)] ) );
clientUsernames.splice(clients.indexOf(e.target),1);
client_list.dataProvider = new DataProvider( clientUsernames );
clients.splice(clients.indexOf(e.target),1);
e.target.close();
e.target.removeEventListener(ProgressEvent.SOCKET_DATA, socketDataHandler);
e.target.removeEventListener(Event.CLOSE, socketDisconnectHandler);
}
private var currentDataStr:String;
private var currentDataType:uint;
private function socketDataHandler(e:ProgressEvent):void {
var socket:Socket = e.currentTarget as Socket;
currentDataType = socket.readUnsignedInt();
currentDataStr = socket.readUTFBytes(socket.bytesAvailable);
send(currentDataType, currentDataStr);
if(currentDataType == 1){
chat_txt.appendText(Constants.mainRef.decrypt(currentDataStr) );
if(autoScroll){
chat_txt.scrollV=chat_txt.maxScrollV;
}
scrollBar.update();
if(soundwarn){
//give sound warning
var snd:Sound = new Sound(new URLRequest("sound/ding.mp3"));
snd.play();
}
}else if(currentDataType == 2){
var tempString:String = Constants.mainRef.decrypt(currentDataStr);
clientUsernames.push(tempString);
client_list.dataProvider = new DataProvider( clientUsernames );
}
tempString=String("");
currentDataStr=String("");
currentDataType=0;
}
private function commandHandler(e:KeyboardEvent):void {
if(e.keyCode==13){
//ENTER GOT PRESSED
var commandStr:String = send_txt.text;
if(commandStr.indexOf(".") == 0){
if(commandStr.indexOf(".del") > -1 ){
chat_txt.text = String("");
currentDataStr = String("");
chat_txt.scrollV=chat_txt.maxScrollV;
scrollBar.update();
}else if(commandStr.indexOf(".burn") > -1 ){
if(chat_txt.length > 0){
send(4,Constants.mainRef.encrypt("burn"));
chat_txt.text = String("");
chat_txt.scrollV=chat_txt.maxScrollV;
scrollBar.update();
}
}else if(commandStr.indexOf(".share") > -1 ){
if(chat_txt.length > 0){
send(5,Constants.mainRef.encrypt(chat_txt.text));
}
}else if(commandStr.indexOf(".ip") > -1 ){
getIpOnline();
}else if(commandStr.indexOf(".roll") > -1 ){
var rngMax:int = parseInt( commandStr.slice(commandStr.indexOf(" ")) );
if(rngMax > 0){
var rawString:String = Constants.mainRef.username + " rolled " + String(Constants.mainRef.randomNum(1,rngMax)) + " out of " + String(rngMax) + "\n";
send(1, Constants.mainRef.encrypt( rawString ) );
chat_txt.appendText( rawString );
if(autoScroll){
chat_txt.scrollV=chat_txt.maxScrollV;
}
scrollBar.update();
}
}else if(commandStr.indexOf(".kick") > -1 && client_list.selectedIndex > -1){
clients[client_list.selectedIndex].close();
clients[client_list.selectedIndex].removeEventListener(ProgressEvent.SOCKET_DATA, socketDataHandler);
clients[client_list.selectedIndex].removeEventListener(Event.CLOSE, socketDisconnectHandler);
clients.splice(client_list.selectedIndex,1);
send(3,Constants.mainRef.encrypt( clientUsernames[client_list.selectedIndex] ) );
clientUsernames.splice(client_list.selectedIndex,1);
client_list.dataProvider = new DataProvider( clientUsernames );
}
}else if(commandStr.length > 0){
send(1, Constants.mainRef.encrypt(Constants.mainRef.username + ": " + commandStr + "\n"));
chat_txt.appendText(Constants.mainRef.username + ": " + commandStr + "\n");
if(autoScroll){
chat_txt.scrollV=chat_txt.maxScrollV;
}
scrollBar.update();
}
send_txt.text = String("");
commandStr = String("");
Constants.STAGE.focus = null;
}
}
public function send(typeIndex:uint, data:String):void {
for each(var client:Socket in clients){
client.writeUnsignedInt(typeIndex);
client.writeUTFBytes(data);
client.flush();
}
}
private function getIpOnline():void{
var phpFileRequest:URLRequest = new URLRequest("https://api.ipify.org/");
phpFileRequest.method = URLRequestMethod.GET;
var phpLoader:URLLoader = new URLLoader();
phpLoader.addEventListener(Event.COMPLETE, showResult);
phpLoader.load(phpFileRequest);
}
private function showResult(e:Event):void{
send_txt.text = String(e.target.data);
e.target.removeEventListener(Event.COMPLETE, showResult);
}
public function die():void{
var i:uint
for(i = 0; i < clients.length; i++) {
clients[i].close();
clients[i].removeEventListener(ProgressEvent.SOCKET_DATA, socketDataHandler);
clients[i].removeEventListener(Event.CLOSE, socketDisconnectHandler);
}
server.removeEventListener(ServerSocketConnectEvent.CONNECT, serverConnectHandler);
server.close();
chat_txt.text = String("");
send_txt.text = String("");
kick_btn.removeEventListener(MouseEvent.CLICK, kickHandler);
ban_btn.removeEventListener(MouseEvent.CLICK, banHandler);
preventSleep_check.removeEventListener(Event.CHANGE, sleepChangeHandler);
close_btn.removeEventListener(MouseEvent.CLICK, closeHandler);
burn_btn.removeEventListener(MouseEvent.CLICK, burnHandler);
share_btn.removeEventListener(MouseEvent.CLICK, shareHandler);
scroll_down_btn.removeEventListener(MouseEvent.CLICK, scrollDownHandler);
soundButton.removeEventListener(MouseEvent.CLICK, soundClickHandler);
autoClean_btn.removeEventListener(Event.CHANGE, autoCleanHandler);
this.removeEventListener(Event.ENTER_FRAME, autoCleanFrameListener);
scrollBar.removeEventListener(Event.SCROLL, scrollingListener);
send_txt.removeEventListener(KeyboardEvent.KEY_DOWN, commandHandler);
clients.length=0;
clientUsernames.length=0;
if(Constants.cardIndexer.length > 0){
for (i = 0; i < Constants.cardIndexer.length; i++) {
Constants.cardIndexer[i].die(4);
}
Constants.cardIndexer.length=0;
Constants.deckRef.length=0;
}
this.parent.removeChild(this);
}
private function chatText(texty:String,chat:Boolean=true):void{
if(chat){
send(1, Constants.mainRef.encrypt(Constants.mainRef.username + ": " + texty + "\n"));
chat_txt.appendText(Constants.mainRef.username + ": " + texty + "\n");
if(autoScroll){
chat_txt.scrollV=chat_txt.maxScrollV;
}
scrollBar.update();
}else{
send(1, Constants.mainRef.encrypt(texty + "\n"));
chat_txt.appendText(texty + "\n");
if(autoScroll){
chat_txt.scrollV=chat_txt.maxScrollV;
}
scrollBar.update();
}
}
}
}
对于那些将来会遇到这个问题的人。 成功托管套接字服务器所需的主要是两件事 互联网:
-端口转发。
-将您的 air 应用程序放入防火墙 "allow" 列表中。 (在我的情况下,这是缺失的部分。哎哟!)
现在一切正常。