websocket兼容低版本IE

flash发起socket通信的三个过程

当封装在页面的flash发起socket通信请求的时候会先寻找服务器端的843端口,获取Crossdomain.xml文件,当服务器没有开启 843的时候,flashPlayer会检查发起请求的swf文件中中有没有使用Security.loadPolicyFile来加载策略文件 Crossdomain.xml,如果还是没有就会看这个发起请求的swf要连接的目标端口有没有策略文件。


  
  Sample of web_socket.js
  
  
  
  

  

  

获取策略文件的Java服务器端代码

package org.david.server.utils;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.io.*;
import java.net.ServerSocket;
import java.net.Socket;

/**
 * @Project server
 * @Author David.Lee
 * @Date 2019/4/3 11:07
 * @Description
 */
public class SecurityXMLServer implements Runnable {

    private static Logger logger = LoggerFactory.getLogger(SecurityXMLServer.class);

    private ServerSocket server;
    private BufferedReader reader;
    private BufferedWriter writer;
    private String xml;

    public SecurityXMLServer()
    {
        /**
         注意此处xml文件的内容,为纯字符串,没有xml文档的版本号
         */
        xml=" "
                +""
                +" ";

        //启动843端口
        createServerSocket(843);
        new Thread(this).start();
    }

    //启动服务器
    private void createServerSocket(int port)
    {
        try {
            server = new ServerSocket(port);
            logger.info("服务监听端口:{}", port);
        } catch (IOException e) {
            System.exit(1);
        }
    }

    //启动服务器线程
    public void run()
    {
        while (true) {
            Socket client = null;
            try {
                //接收客户端的连接
                client = server.accept();

                InputStreamReader input = new InputStreamReader(client.getInputStream(), "UTF-8");
                reader = new BufferedReader(input);
                OutputStreamWriter output = new OutputStreamWriter(client.getOutputStream(), "UTF-8");
                writer = new BufferedWriter(output);

                //读取客户端发送的数据
                StringBuilder data = new StringBuilder();
                int c = 0;
                while ((c = reader.read()) != -1)
                {
                    if (c != '\0')
                        data.append((char) c);
                    else
                        break;
                }
                String info = data.toString();
                //接收到客户端的请求之后,将策略文件发送出去
                if(info.indexOf("") >=0)
                {
                    writer.write(xml + "\0");
                    writer.flush();
                    logger.info("将安全策略文件发送至: {}", client.getInetAddress());
                }
                else
                {
                    writer.write("请求无法识别\0");
                    writer.flush();
                    logger.info("请求无法识别: {}", client.getInetAddress());
                }
                client.close();
            } catch (Exception e) {
                e.printStackTrace();
                try {
                    //发现异常关闭连接
                    if (client != null) {
                        client.close();
                        client = null;
                    }
                } catch (IOException ex) {
                    ex.printStackTrace();
                } finally {
                    //调用垃圾收集方法
                    System.gc();
                }
            }
        }
    }

    //测试主函数
    public static void main(String[] args)
    {
        new SecurityXMLServer();
    }
}

代码下载地址:
链接:https://pan.baidu.com/s/19po_pm4j4CEFHbiASfamUQ
提取码:nwe6

你可能感兴趣的:(websocket兼容低版本IE)