1
我有一個Groovy服務器和Flex客戶端。當我嘗試從Flex連接到Groovy應用程序時,我感受到<policy-file-request/>
消息。然後我發送一些策略XML響應並嘗試將某些內容發送到服務器。但不是在Groovy方面收到什麼東西,我什麼都沒有。幾秒鐘後,我在Flex應用程序端獲得了SecurityException
。Flex套接字連接
Groovy的服務器代碼:
import java.util.Scanner
class server
{
public static handleConnection(client)
{
PrintWriter socketwriter = new PrintWriter(client.getOutputStream())
Scanner socketreader = new Scanner(client.getInputStream())
while (socketreader.hasNext())
{
String s = socketreader.nextLine()
println s
if (s =~ /<policy-file-request\s*\/>/)
{
socketwriter.print("<?xml version=\"1.0\"?><!DOCTYPE cross-domain-policy SYSTEM \"/xml/dtds/cross-domain-policy.dtd\"><cross-domain-policy><site-control permitted-cross-domain-policies=\"master-only\" /><allow-access-from domain=\"*\" to-ports=\"2900-2910\" /></cross-domain-policy>\n\0\n")
socketwriter.flush()
socketwriter.close()
client.close()
println "Policy File sent."
return
}
}
socketwriter.print("Connection: Done.\n")
socketwriter.flush()
socketwriter.close()
socketreader.close()
}
static main(args)
{
ServerSocket serversocket
try
{
serversocket = new ServerSocket(2900)
println "Server is up and running"
while (true)
{
Socket client = serversocket.accept()
Thread.start {
server.handleConnection(client)
}
}
} catch (e)
{
e.printStackTrace()
} finally
{
serversocket.close()
}
}
}
Flex客戶端代碼:
<?xml version="1.0" encoding="utf-8"?>
<mx:Application xmlns:mx="http://www.adobe.com/2006/mxml" layout="absolute">
<mx:Script>
<![CDATA[
import flash.net.Socket;
import flash.events.*;
import mx.controls.Alert;
private var socket : Socket;
private function connect() : void
{
socket = new Socket();
socket.addEventListener(Event.CONNECT, onConnect);
socket.addEventListener(ProgressEvent.SOCKET_DATA, onSocketData);
socket.addEventListener(SecurityErrorEvent.SECURITY_ERROR, ServerSecurityError);
socket.connect("localhost", 2900);
Alert.show("yay!");
}
private function ServerSecurityError(evt : Event) : void
{
Alert.show("security error")
}
private function onConnect(evt : Event) : void
{
Alert.show("Connected to server!");
}
private function onSocketData(evt : ProgressEvent) : void
{
var data : String = socket.readUTFBytes(socket.bytesAvailable);
Alert.show("Read: " + data);
}
private function send() : void
{
var msg : String = "Hello, World!";
socket.writeUTFBytes(msg);
socket.flush();
Alert.show("Sent: " + msg);
}
]]>
</mx:Script>
<mx:Button label="connect" click="connect()" x="10" y="10" />
<mx:Button label="send something" click="send()" x="10" y="40" />
</mx:Application>
所以,問題是:我應該怎麼連接來獲得服務器端和客戶端之間穩定的通信?
我想你應該給我們提供完整的錯誤信息。我的猜測是這是一個跨域問題。 SWF是否在常規服務器上提供服務? – JeffryHouser
@ www.Flextras.com感謝您的回覆。我在'ServerSecurityError'方法中得到錯誤消息。你有什麼想法如何我可以在那裏得到錯誤消息? – shybovycha
所有相關信息都應在發送到該方法的事件中。 – JeffryHouser