Reputation: 41
I really hope someone can shed some light on this. I am building a really simple Chat server/client using PHP and Flex. I have followed many tutorials and have a working server in PHP and a working client in Flex, except that if I use the client on Windows I can't send any messages.
When I send a message on my Mac it goes through to the server and gets sent to all the clients, including any Windows client I may have connected. The message will actually display on Windows clients as well, it's only when I try sending from Windows that it doesn't work.
I've tried adding a crossdomain.xml file served by a PHP script listening on the correct port, but none of the clients ever seem to ask for it, and if the client works on Mac I assumed it should work on Windows. By the way I'm exporting the project as an AIR file (dunno if that makes a difference).
I can't find anything regarding this so I'm wondering if I'm being stupid somewhere or whether I need to takes some special measures for Windows clients?
I have pasted my client code below (I have never posted on Stack Overflow so I apologize for the lack of code formatting, if possible could someone explain how to fix it?).
import flash.events.Event;
import flash.events.IOErrorEvent;
import flash.events.MouseEvent;
import flash.events.ProgressEvent;
import flash.events.SecurityErrorEvent;
import flash.net.Socket;
public var socket:Socket;
// Run on windowComplete
protected function init():void {
btnConnect.addEventListener(MouseEvent.CLICK, onBtnConnectClick);
}
// Run on closing
protected function deinit():void {
if ( (socket != null) && (socket.connected) ) {
socket.close();
}
}
// Called when the "Connect" button is clicked
protected function onBtnConnectClick(e:MouseEvent):void {
if (txtServer.text != '') {
socket = new Socket();
socket.addEventListener(Event.CONNECT, onSocketConnect);
socket.addEventListener(ProgressEvent.SOCKET_DATA, onSocketDataProgress);
socket.addEventListener(IOErrorEvent.IO_ERROR, onSocketIOError);
socket.addEventListener(SecurityErrorEvent.SECURITY_ERROR, onSocketSecurityError);
socket.connect(txtServer.text, new uint(txtPort.text));
btnSend.addEventListener(MouseEvent.CLICK, onBtnSendClick);
}
}
// Called when the "Send" button is clicked
protected function onBtnSendClick(e:MouseEvent):void {
if (txtChatMessage.text != '') {
txtChatWindow.text += 'You: '+ txtChatMessage.text +"\n";
socket.writeUTFBytes(txtUsername.text +': '+ txtChatMessage.text);
}
txtChatMessage.text = '';
}
// Called when the socket is connected
protected function onSocketConnect(e:Event):void {
txtChatWindow.text += 'Connected\n\n';
btnConnect.label = "Disconnect";
btnConnect.removeEventListener(MouseEvent.CLICK, onBtnConnectClick);
btnConnect.addEventListener(MouseEvent.CLICK, onSocketDisconnect);
}
// Called when the socket recieves data
protected function onSocketDataProgress(e:ProgressEvent):void {
var data:String = socket.readUTFBytes(socket.bytesAvailable);
txtChatWindow.text += data +"\n";
}
// Called when there is an IO Error on the socket
protected function onSocketIOError(e:IOErrorEvent):void {
txtChatWindow.text += e.text;
}
// Called when there is a security error on the socket
protected function onSocketSecurityError(e:SecurityErrorEvent):void {
txtChatWindow.text += e.text;
}
// Called when the "Disconnect" button is clicked
protected function onSocketDisconnect(e:MouseEvent):void {
if ( (socket != null) && (socket.connected) ) {
socket.close();
}
btnConnect.label = "Connect";
btnConnect.addEventListener(MouseEvent.CLICK, onBtnConnectClick);
}
Upvotes: 0
Views: 557
Reputation: 41
AHA!! I figured it out. After a lot of searching I noticed a tiny comment which confirmed my suspicions that Windows needed some extra code, one line to be exact.
In Mac and Linux, flush() is called implicitly between execution frames, however, on Windows, data is never sent unless you call flush().
So the onBtnSendClick function now looks like this:
protected function onBtnSendClick(e:MouseEvent):void {
if (txtChatMessage.text != '') {
txtChatWindow.text += 'You: '+ txtChatMessage.text +"\n";
socket.writeUTFBytes(txtUsername.text +': '+ txtChatMessage.text);
socket.flush();
}
txtChatMessage.text = '';
}
Upvotes: 4