是的。flash.net.Socket
您的客户端应通过( doc ) 或flash.net.XMLSocket
( doc )连接到服务器。在大多数情况下,您只需要一台可以连接许多客户端的服务器。不完全清楚为什么两端都有服务器?
编辑:
下面是一个客户端使用Socket
. 这会连接到服务器,然后(无限期地)等待数据。当接收到一大块数据时(在这种情况下,我期望字符串以换行符终止),数据将与事件一起传递。
如果您尝试过这种方法但没有成功,您应该查看是否有任何错误或连接是否关闭。
文档类:
package {
import flash.display.Sprite;
public class Main extends Sprite {
protected var socket:SocketTest;
public function Main() {
this.socket = new SocketTest();
this.socket.addEventListener(SocketMessageEvent.MESSAGE_RECEIVED, onSocketMessage);
this.socket.connect("127.0.0.1", 7001);
}
protected function onSocketMessage(e:SocketMessageEvent):void {
var date:Date = new Date();
trace(date.hoursUTC + ":" + date.minutesUTC + ":" + date.secondsUTC + " Incoming message: " + e.message);
}
}
}
SocketTest 类:
package {
import flash.net.Socket;
import flash.events.Event;
import flash.events.IOErrorEvent;
import flash.events.SecurityErrorEvent;
import flash.events.ProgressEvent;
import flash.errors.IOError;
public class SocketTest extends Socket {
protected var _message:String;
public function SocketTest() {
super();
this._message = "";
this.addEventListener(Event.CONNECT, socketConnected);
this.addEventListener(Event.CLOSE, socketClosed);
this.addEventListener(ProgressEvent.SOCKET_DATA, socketData);
this.addEventListener(IOErrorEvent.IO_ERROR, socketError);
this.addEventListener(SecurityErrorEvent.SECURITY_ERROR, socketError);
}
protected function socketData(event:ProgressEvent):void {
var str:String = readUTFBytes(bytesAvailable);
//For this example, look for \n as a message terminator
var messageParts:Array = str.split("\n");
//There could be multiple messages or a partial message,
//pass on complete messages and buffer any partial
for (var i = 0; i < messageParts.length; i++) {
this._message += messageParts[i];
if (i < messageParts.length - 1) {
this.notifyMessage(this._message);
this._message = "";
}
}
}
protected function notifyMessage(value:String):void {
this.dispatchEvent(new SocketMessageEvent(SocketMessageEvent.MESSAGE_RECEIVED, value));
}
protected function socketConnected(event:Event):void {
trace("Socket connected");
}
protected function socketClosed(event:Event):void {
trace("Connection was closed");
//TODO: Reconnect if needed
}
protected function socketError(event:Event):void {
trace("An error occurred:", event);
}
}
}
SocketMessageEvent 类:
package {
import flash.events.Event;
public class SocketMessageEvent extends Event {
public static const MESSAGE_RECEIVED:String = "messageReceived";
protected var _message:String;
public function SocketMessageEvent(type:String, message:String = "", bubbles:Boolean = false, cancelable:Boolean = false) {
super(type, bubbles, cancelable);
this._message = message;
}
public function get message():String {
return this._message;
}
}
}
作为测试,我将服务器设置为每 5 秒发送一条消息,这是控制台中的输出:
Socket connected
21:36:24 Incoming message: Message from server: 0
21:36:29 Incoming message: Message from server: 1
21:36:34 Incoming message: Message from server: 2
21:36:39 Incoming message: Message from server: 3
21:36:44 Incoming message: Message from server: 4
21:36:49 Incoming message: Message from server: 5