var websocket;
var url = '';
$(function(){
url = encodeURI('wss://'+'${oladress }');
createWebSocket(url);
}
window.onbeforeunload = function() {
websocket.close();
}
var lockReconnect = false;
function reconnect(url) {
if(lockReconnect) return;
lockReconnect = true;
setTimeout(function () {
createWebSocket(url);
lockReconnect = false;
}, 2000);
}
function createWebSocket(url) {
try{
if('WebSocket' in window){
websocket = new WebSocket(url);
}else if('MozWebSocket' in window){
websocket = new MozWebSocket(url);
}else{
layui.use(['layer'],function(){
var layer = layui.layer;
layer.alert("您的浏览器不支持websocket协议,建议使用新版谷歌、火狐等浏览器,请勿使用IE10以下浏览器,360浏览器请使用极速模式,不要使用兼容模式!");
});
}
online();
}catch(e){
reconnect(url);
console.log(e);
}
}
var heartCheck = {
timeout: 60000,
timeoutObj: null,
serverTimeoutObj: null,
reset: function(){
clearTimeout(this.timeoutObj);
clearTimeout(this.serverTimeoutObj);
return this;
},
start: function(){
var self = this;
this.timeoutObj = setTimeout(function(){
websocket.send("ping");
self.serverTimeoutObj = setTimeout(function(){
websocket.close();
}, self.timeout)
}, this.timeout)
}
}
function online(){
websocket.onopen = function() {
heartCheck.reset().start();
websocket.send('[join]'+user);
};
websocket.onerror = function() {
reconnect(url);
};
websocket.onclose = function() {
reconnect(url);
};
websocket.onmessage = function(message) {
heartCheck.reset().start();
if(message.data == 'ping'){
return;
}
var message = JSON.parse(message.data);
if(message.type == 'goOut'){
$("body").html("");
goOut("1");
}else if(message.type == 'thegoout'){
return;
}else if(message.type == 'senFhsms'){
fhsmsCount = Number(fhsmsCount)+1;
$("#fhsmsCount").html(Number(fhsmsCount));
$("#fhsmsobj").html('<audio style="display: none;" id="fhsmstsy" src="static/sound/'+TFHsmsSound+'.mp3" autoplay controls></audio>');
$("#fhsmstss").tips({
side:3,
msg:'您有新消息',
bg:'#AE81FF',
time:30
});
}else if(message.type == 'fhtask'){
if(message.RNUMBER == 'no'){
$("#fhsmsobj").html('<audio style="display: none;" id="fhsmstsy" src="static/sound/'+TFHsmsSound+'.mp3" autoplay controls></audio>');
topTask();
}else{
$.ajax({
type: "POST",
url: locat+'/head/isNowRole.do',
data: {RNUMBER:message.RNUMBER,tm:new Date().getTime()},
dataType:'json',
cache: false,
success: function(data){
if('yes' == data.msg){
$("#fhsmsobj").html('<audio style="display: none;" id="fhsmstsy" src="static/sound/'+TFHsmsSound+'.mp3" autoplay controls></audio>');
topTask();
}
}
});
}
}
};
}
package com.fh.plugin.websocketOnline;
import java.io.IOException;
import java.net.InetSocketAddress;
import java.net.UnknownHostException;
import net.sf.json.JSONObject;
import org.java_websocket.WebSocket;
import org.java_websocket.WebSocketImpl;
import org.java_websocket.framing.Framedata;
import org.java_websocket.handshake.ClientHandshake;
import org.java_websocket.server.WebSocketServer;
public class OnlineChatServer extends WebSocketServer{
public OnlineChatServer(int port) throws UnknownHostException {
super(new InetSocketAddress(port));
}
public OnlineChatServer(InetSocketAddress address) {
super(address);
}
@Override
public void onOpen( WebSocket conn, ClientHandshake handshake ) {
}
@Override
public void onClose( WebSocket conn, int code, String reason, boolean remote ) {
userLeave(conn);
}
@Override
public void onMessage(WebSocket conn, String message){
message = message.toString();
if(null != message && message.startsWith("[join]")){
this.userjoin(message.replaceFirst("\\[join\\]", ""),conn);
}else if(null != message && message.startsWith("[goOut]")){
this.goOut(message.replaceFirst("\\[goOut\\]", ""));
}else if(null != message && message.startsWith("[fhsms]")){
this.senFhsms(message.replaceFirst("\\[fhsms\\]", ""));
}else if(null != message && message.startsWith("[fhtask]")){
this.senFhTask(message.replaceFirst("\\[fhtask\\]", ""));
}else if(null != message && message.startsWith("[leave]")){
this.userLeave(conn);
}else if(null != message && message.startsWith("[count]")){
this.getUserCount(conn);
}else if(null != message && message.startsWith("[ConnectionSuccess]")){
OnlineChatServerPool.setFhadmin(conn);
this.getUserList();
}else if(null != message && message.equals("ping")) {
}else{
OnlineChatServerPool.sendMessageToUser(conn, message);
}
}
public void onFragment( WebSocket conn, Framedata fragment ) {
}
@Override
public void onError( WebSocket conn, Exception ex ) {
if( conn != null ) {
}
}
public void userjoin(String user, WebSocket conn){
onlineMaganger(1,user,conn);
}
public void senFhsms(String user){
JSONObject result = new JSONObject();
result.element("type", "senFhsms");
OnlineChatServerPool.sendMessageToUser(OnlineChatServerPool.getWebSocketByUser(user),result.toString());
}
public void senFhTask(String user){
JSONObject result = new JSONObject();
result.element("type", "fhtask");
WebSocket ws = OnlineChatServerPool.getWebSocketByUser(user);
if(null != ws){
result.element("RNUMBER", "no");
OnlineChatServerPool.sendMessageToUser(ws,result.toString());
}else{
result.element("RNUMBER", user);
OnlineChatServerPool.sendMessage(result.toString());
}
}
public void goOut(String user){
this.goOut(OnlineChatServerPool.getWebSocketByUser(user),"thegoout");
}
public void goOut(WebSocket conn,String type){
JSONObject result = new JSONObject();
result.element("type", type);
result.element("msg", "goOut");
OnlineChatServerPool.sendMessageToUser(conn,result.toString());
}
public void userLeave(WebSocket conn){
onlineMaganger(2,null,conn);
}
public void getUserCount(WebSocket conn){
JSONObject result = new JSONObject();
result.element("type", "count");
result.element("msg", OnlineChatServerPool.getUserCount());
OnlineChatServerPool.sendMessageToUser(conn,result.toString());
}
public void getUserList(){
WebSocket conn = OnlineChatServerPool.getFhadmin();
if(null == conn){return;}
JSONObject result = new JSONObject();
result.element("type", "userlist");
result.element("list", OnlineChatServerPool.getOnlineUser());
OnlineChatServerPool.sendMessageToUser(conn,result.toString());
}
public synchronized void onlineMaganger(int type,String user, WebSocket conn){
if(type == 1){
if(null == OnlineChatServerPool.getWebSocketByUser(user)){
OnlineChatServerPool.addUser(user,conn);
addUserToFhadmin(user);
}else{
OnlineChatServerPool.addUser(user,conn);
addUserToFhadmin(user);
}
}else{
OnlineChatServerPool.removeUser(conn);
this.getUserList();
}
}
public void addUserToFhadmin(String user){
WebSocket conn = OnlineChatServerPool.getFhadmin();
if(null == conn){return;}
JSONObject result = new JSONObject();
result.element("type", "addUser");
result.element("user", user);
OnlineChatServerPool.sendMessageToUser(conn,result.toString());
}
public static void main( String[] args ) throws InterruptedException , IOException {
WebSocketImpl.DEBUG = false;
int port = 8887;
OnlineChatServer s = new OnlineChatServer(port);
s.start();
}
}