Flex与java通过socket进行交互出现安全沙箱问题
用java写的服务端代码,用flex写的客户端代码,是做一个曲线图的实时效果。flex发送连接,java端接收到套接字,则不停的sleep数据,想数据以json格式返回给flex,flex解开json数据,将数据绑定lineChart组件,实现实时刷新。
以上过程一切正常OK。当我以为一切都好的时候,大问题来了。
我把flex下面的bin-debug目录全部拷贝到我的java的web工程(真正要部署到服务器上的程序)下面,然后用html的object标签去调用bin-debug目录里面的.swf文件。启动tomcat,访问页面,报安全沙箱错误。界面出来了,但是没有数据,连接中断(数据肯定没有问题,flex单独和java交互的时候已经验证了这一点,只是拷贝到web工程下面才报的)。
我的解决方法1.
在tomcat的Root目录下面放crossdomain.xml文件。
里面的内容是
<?xml version="1.0"?>
<!DOCTYPE cross-domain-policy SYSTEM "http://www.macromedia.com/xml/dtds/cross-domain-policy.dtd";>
<cross-domain-policy>
<allow-access-from domain="*" secure="false"/>
</cross-domain-policy>
但是此方法还是不行。
以下是我的代码,跪求帮忙
<?xml version="1.0"?>
<!-- Simple example to demonstrate the LineChart and AreaChart controls. -->
<s:Application xmlns:fx="http://ns.adobe.com/mxml/2009"
xmlns:s="library://ns.adobe.com/flex/spark"
xmlns:mx="library://ns.adobe.com/flex/mx" creationComplete="initApp();">
<fx:Script>
<![CDATA[
import com.adobe.serialization.json.JSON;
import flash.net.Socket;
import flash.utils.ByteArray;
import mx.collections.ArrayCollection;
import mx.controls.Alert;
import mx.rpc.events.ResultEvent;
public var dataArray:Array;
[Bindable]
private var expensesAC:ArrayCollection = null;
private var componnets_num:String = "A1000301";
private var socket:Socket=new Socket(); //定义Socket,准备套接字
//初始化程序
internal function initApp():void
{
//Security.loadPolicyFile("xmlsocket://113.115.172.98:843");
//Security.loadPolicyFile("xmlsocket://localhost:8088/crossdomain.xml"); //解决安全沙箱问题
socket.connect("localhost",9100); //连接服务器
socket.addEventListener(Event.CONNECT,funConnect); //监听是否连接socket.addEventListener(ProgressEvent.SOCKET_DATA, onSocketData ); //接收服务端推过来的数据
socket.addEventListener(Event.CLOSE,funClose); //监听连接关闭
}
internal function funConnect(event:Event):void
{
//sendMessage(componnets_num);
}
internal function funClose(event:Event):void
{
//Alert.show("连接已关闭","OK");
}
//internal function sendMessage(msg:String):void//发送数据
//{
//var message:ByteArray=new ByteArray();//新建一个ByteArray存放数据
//message.writeUTFBytes(msg +"\r\n");//写入数据,writeUTFBytes方法,以utf-8格式传数据避免中文乱码
//socket.writeBytes(message); //写入Socket的缓冲区
//socket.flush(); //调用flush方法发送信息
//}
private function onSocketData(event:ProgressEvent):void {
var myText:String = "";
while(socket.bytesAvailable) {
myText+=socket.readMultiByte(socket.bytesAvailable, "utf8");
dataArray = JSON.decode(myText) as Array; // 此针对数据类型, 因为我的这个是返回的JSON \
//将得到的数据用ArrayCollection封装起来
expensesAC = new ArrayCollection(dataArray);
// 更新linechart组件信息
linechart.dataProvider = expensesAC;
}
}
]]>
</fx:Script>
<fx:Declarations>
<!-- Define custom colors for use as fills in the AreaChart control. -->
<mx:SolidColor id="sc1" color="blue" alpha=".3"/>
<mx:SolidColor id="sc2" color="red" alpha=".3"/>
<!-- Define custom Strokes. -->
<mx:SolidColorStroke id = "s1" color="blue" weight="1"/>
<mx:SolidColorStroke id = "s2" color="red" weight="1" />
</fx:Declarations>
<mx:Panel
height="100%" width="100%" layout="horizontal" headerHeight="0">
<mx:LineChart id="linechart" width="500" height="300" dataProvider="{expensesAC}"
showDataTips="true">
<mx:horizontalAxis>
<mx:CategoryAxis categoryField="time"/>
</mx:horizontalAxis>
<mx:series>
<mx:LineSeries yField="down_traffic" form="curve" displayName="Realtek/811D" lineStroke="{s1}" itemRenderer="mx.charts.renderers.CircleItemRenderer"/>
<mx:LineSeries yField="up_traffic" form="curve" displayName="11b/g/n" lineStroke="{s2}" itemRenderer="mx.charts.renderers.CircleItemRenderer"/>
</mx:series>
</mx:LineChart>
<mx:Legend dataProvider="{linechart}"/>
</mx:Panel>
</s:Application>
java代码
package com.kayak.monitPlatform.socket;
import java.net.*;
import java.io.*;
import java.util.*;
import com.kayak.monitPlatform.componnets.entity.NetworkInfo;
import com.kayak.monitPlatform.inter.IClientHandler;
import com.kayak.monitPlatform.registration.Registration;
import net.sf.json.JSONArray;
import net.sf.json.JSONObject;
public class SocketService {
private ServerSocket server;
List<Socket> sManager = new ArrayList<Socket>();
private String xml = "";
public SocketService() {
}
void startServer()// 运行服务器
{
try {
//xml = "<cross-domain-policy><site-control permitted-cross-domain-policies='all'/>"
//+ "<allow-access-from domain=\"*\" to-ports=\"*\"/>"
//+ "</cross-domain-policy> ";
server = new ServerSocket(9100);
System.out.println("服务器套接字已创建成功!");
while (true) {
Socket socket = server.accept();
System.out.println("已经与客户端连接");
new J_Thread(socket).start();
sManager.add(socket);
System.out.println("当前客户端连结数:" + sManager.size());
}
} catch (Exception e) {
} finally {
try {
server.close();
} catch (Exception e) {
}
}
}
public static void main(String[] args) {
SocketService server = new SocketService();
server.startServer();
}
class J_Thread extends Thread// 与客户端进行通信的线程类
{
Socket socket;// 套接字引用变量
private BufferedReader reader;// 套接字输入流
private BufferedWriter writer;// 套接字输出流
J_Thread(Socket socket)// 构造函数
{
this.socket = socket;
}
public void run() {
try {
while (true) {
// 输入
reader = new BufferedReader(new InputStreamReader(socket
.getInputStream(), "UTF-8"));
//这里会根本组件编号去读取数据
JSONArray jsonArray = new IClientHandler().doCommand();
// 输出
writer = new BufferedWriter(new OutputStreamWriter(socket
.getOutputStream())); // 获取套接字输出流
writer.write(jsonArray.toString());
writer.flush();
sleep(1000);
}
} catch (Exception e) {
e.printStackTrace();
} finally {
try {
sManager.remove(socket);// 删除套接字
// 关闭输入输出流及套接字
if (reader != null)
reader.close();
if (writer != null)
writer.close();
if (socket != null)
socket.close();
reader = null;
writer = null;
socket = null;
System.out.println("客户端离开");
System.out.println("当前客户端的连接数:" + sManager.size());
} catch (Exception e) {
e.printStackTrace();
}
}
}
}
}
以上都是我未加安全策略的代码,因为我加了也不行。在这里请教大家乐,大家可以在我代码上面加东西,告诉我该怎么做。诚心求助,谢谢
[解决办法]
socket的安全沙箱错误 网上找下需要特殊的配置。不是简单的在tomcat下方策略文件。
[解决办法]
帮忙.............................