Websocket兼容ie10以下版本,使用flash的方式发送websocket

在tomcat自带的chat例子的基础上,使用https://github.com/gimite/web-socket-js

在java端新建FlashPolicyServer用来传输flash协议

package com.hikvision.msm.adapter.websocket;

import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.ServerSocket;
import java.net.Socket;
import org.springframework.stereotype.Component;

import com.hikvision.msm.common.util.log.LogUtil;

@Component
public class FlashPolicyServer {
    private ServerSocket serverSocket;

    private static Thread serverThread;

    private int port;

    private static boolean listening = true;

    public FlashPolicyServer() {
        this(843);
        start();
    }

    public FlashPolicyServer(int port) {
        this.port = port;
        start();
    }

    public void start() {
        try {
            serverThread = new Thread(new Runnable() {
                public void run() {
                    try {
                        LogUtil.info("FlashPolicyServer: Starting...");
                        serverSocket = new ServerSocket(port);

                        while (listening) {
                            final Socket socket = serverSocket.accept();

                            Thread t = new Thread(new Runnable() {
                                public void run() {
                                    try {

                                        LogUtil.debug("FlashPolicyServer: Handling Request...");

                                        socket.setSoTimeout(10000);

                                        InputStream in = socket.getInputStream();

                                        byte[] buffer = new byte[23];

                                        if (in.read(buffer) != -1 && (new String(buffer, "ISO-8859-1"))
                                                .startsWith("<policy-file-request/>")) {

                                            LogUtil.debug("FlashPolicyServer: Serving Policy File...");

                                            OutputStream out = socket.getOutputStream();

                                            byte[] bytes = ("<?xml version=\"1.0\"?>\n"
                                                    + "<!DOCTYPE cross-domain-policy SYSTEM \"/xml/dtds/cross-domain-policy.dtd\">\n"
                                                    + "<cross-domain-policy> \n"
                                                    + "   <site-control permitted-cross-domain-policies=\"master-only\"/>\n"
                                                    + "   <allow-access-from domain=\"*\" to-ports=\"*\" />\n"
                                                    + "</cross-domain-policy>").getBytes("ISO-8859-1");

                                            out.write(bytes);

                                            out.write(0x00);

                                            out.flush();
                                            out.close();
                                        } else {
                                            LogUtil.debug("FlashPolicyServer: Ignoring Invalid Request:"+new String(buffer));
                                        }
                                    } catch (Exception e) {
                                        LogUtil.error(e.getMessage(), e);
                                    }finally {
                                        try {
                                            socket.close();
                                        } catch (Exception ex2) {
                                            LogUtil.error(ex2.getMessage(), ex2);
                                        }
                                    }
                                }
                            });
                            t.start();
                        }
                    } catch (IOException ex) {
                        LogUtil.error("FlashPolicyServer Error---");
                        LogUtil.error(ex.getMessage(), ex);
                    }
                }

            });
            serverThread.start();
        } catch (Exception ex) {
            LogUtil.error("FlashPolicyServer Error---");
            LogUtil.error(ex.getMessage(), ex);
        }
    }

    public void stop() {
        LogUtil.info("FlashPolicyServer: Shutting Down...");

        if (listening) {
            listening = false;
        }

        if (!serverSocket.isClosed()) {
            try {
                serverSocket.close();
            } catch (Exception ex) {
                LogUtil.error(ex.getMessage(), ex);
            }
        }
    }
}
前端引入js及swf文件

myWebSocket.js

"use strict";
//Let the library know where WebSocketMain.swf is:
var WEB_SOCKET_SWF_LOCATION = "/app/src/scripts/common/WebSocketMain.swf";

var Chat = {};

Chat.socket = null;

Chat.connect = (function(host) {
    Chat.socket = new WebSocket(host);
//    if ('WebSocket' in window) {
//        Chat.socket = new WebSocket(host);
//    } else if ('MozWebSocket' in window) {
//        Chat.socket = new MozWebSocket(host);
//    } else {
//        Console.log('Error: WebSocket is not supported by this browser.');
//        return;
//    }

    Chat.socket.onopen = function() {
        Console.log('Info: WebSocket connection opened.');
    };

    Chat.socket.onclose = function() {
        Console.log('Info: WebSocket closed.');
    };

    Chat.socket.onmessage = function(message) {
        Console.log(message.data);
    };
});

Chat.initialize = function() {
    if (window.location.protocol == 'http:') {
        Chat.connect('ws://' + window.location.host + '/websocket/myWebsocket');
    } else {
        Chat.connect('wss://' + window.location.host + '/websocket/myWebsocket');
    }
};

Chat.sendMessage = (function() {
    var message = document.getElementById('chat').value;
    if (message != '') {
        Chat.socket.send(message);
        document.getElementById('chat').value = '';
    }
});

var Console = {};

Console.log = (function(message) {
    console.log(message);
});

Chat.initialize();

转载于:https://my.oschina.net/ffse54s/blog/902579

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值