在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();