1. 程式人生 > >基於html5websocket java實現簡單通訊(雖然通了但是...你懂得)

基於html5websocket java實現簡單通訊(雖然通了但是...你懂得)

很早就想寫一下基於Html5 websocket通訊的但是一直沒有下手,今天心血來潮試了一下,原來以為是html5 socket的所以直接用以前自己弄得socket java服務來當服務端,百度了別人的html前臺的,結果不行...只好認慫,把別人的服務端程式碼也拷下來試下,結果一看原來是websocket...跟socket兩種性質。

然後試了下,通是通了,但是他的只能群發,我想要單播的,想了一下,想出了一個很笨很笨的方法,在連線的時候把使用者的Id作為唯一標識傳給服務端,服務端儲存,然後根據使用者要傳送的物件的id進行比對,相同就傳送.恩...效果是實現了.

恩,挺開心...突然瞥到關閉那邊,只是把連線移除,沒有把使用者移除,加上...測通

以下程式碼參考 http://blog.csdn.net/huangwuyi/article/details/12912283 要加兩個包catalina.jar、tomcat-coyote.jar 

如果報錯Java.lang.NoSuchMethodException: org.apache.catalina.deploy.WebXml addFilter 在Tomacat7的context.xml檔案裡的<Context>中加上<Loader delegate="true" />

前臺頁面:

<html> 
    <head> 
        <title>WebSoket Demo</title> 
        <script type="text/JavaScript"> 
            //驗證瀏覽器是否支援WebSocket協議
            if (!window.WebSocket) { 
                alert("WebSocket not supported by this browser!"); 
            } 
             var ws;
            function display() { 
                //var valueLabel = document.getElementById("valueLabel"); 
                //valueLabel.innerHTML = ""; 
                ws=new WebSocket("ws://localhost:8080/Socket/websocket"); 
                //監聽訊息
                ws.onmessage = function(event) { 
                    //valueLabel.innerHTML+ = event.data; 
                    log(event.data);
                }; 
                // 開啟WebSocket 
                ws.onclose = function(event) { 
                  //WebSocket Status:: Socket Closed
                }; 
                // 開啟WebSocket
                ws.onopen = function(event) { 
                   //WebSocket Status:: Socket Open
                    //// 傳送一個初始化訊息
                    ws.send("Hello, Server!"+"%%%4"); //連線websocket 傳遞userId
                }; 
                ws.onerror =function(event){
                    //WebSocket Status:: Error was reported
                };
            } 
            var log = function(s) {  
   if (document.readyState !== "complete") {  
       log.buffer.push(s);  
   } else {  
       document.getElementById("contentId").innerHTML += (s + "\n");  
   }  
}  
            function sendMsg(){
                var msg=document.getElementById("messageId");
                //alert(msg.value);
                ws.send(msg.value); 
            }
        </script> 
    </head> 
    <body onload="display();"> 
        <div id="valueLabel"></div> 
        <textarea rows="20" cols="30" id="contentId"></textarea>
        <br/>
        <input name="message" id="messageId"/>
        <button id="sendButton" onClick="javascript:sendMsg()" >Send</button>
    </body> 
</html> 

傳送資訊的時候帶上id根據特定的格式拼湊,服務端拆分.

服務端:

web.xml

<?xml version="1.0" encoding="UTF-8"?>
<web-app version="3.0" xmlns="http://java.sun.com/xml/ns/javaee"
	xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
	xsi:schemaLocation="http://java.sun.com/xml/ns/javaee 
	http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd">
	<display-name></display-name>
	<servlet>
		<servlet-name>initServlet</servlet-name>
		<servlet-class>com.InitServlet</servlet-class>
		<load-on-startup>1</load-on-startup>
	</servlet>
	<servlet>
		<servlet-name>websocket</servlet-name>
		<servlet-class>com.TestWebSocketServlet</servlet-class>
	</servlet>
	<servlet-mapping>
		<servlet-name>websocket</servlet-name>
		<url-pattern>/websocket</url-pattern>
	</servlet-mapping>
</web-app>

InitServlet
package com;

import java.util.ArrayList;
import java.util.List;

import javax.servlet.ServletConfig;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;

import org.apache.catalina.websocket.MessageInbound;

public class InitServlet extends HttpServlet {

	private static final long serialVersionUID = 1L;
	private static List<MessageInbound> socketList;

	public void init(ServletConfig config) throws ServletException {
		InitServlet.socketList = new ArrayList<MessageInbound>();
		super.init(config);
		System.out.println("Server start============");
	}

	public static List<MessageInbound> getSocketList() {
		return InitServlet.socketList;
	}

}

TestWebSocketServlet
package com;

import java.io.IOException;
import java.nio.ByteBuffer;
import java.nio.CharBuffer;
import java.util.ArrayList;
import java.util.List;

import javax.servlet.http.HttpServletRequest;

import org.apache.catalina.websocket.MessageInbound;
import org.apache.catalina.websocket.StreamInbound;
import org.apache.catalina.websocket.WebSocketServlet;
import org.apache.catalina.websocket.WsOutbound;

public class TestWebSocketServlet extends WebSocketServlet {

	/**
* 
*/
	private static final long serialVersionUID = 1L;
	// 儲存連結的容器
	private static List<WebSocketMessageInbound> connsList = new ArrayList<WebSocketMessageInbound>();

	// 儲存使用者資訊
	private static List<String> userIds = new ArrayList<String>();

	@Override
	protected StreamInbound createWebSocketInbound(String subProtocol, HttpServletRequest request) {
		// TODO Auto-generated method stub
		return new WebSocketMessageInbound();
	}

	public class WebSocketMessageInbound extends MessageInbound {

		@Override
		protected void onClose(int status) {
			// InitServlet.getSocketList().remove(this);
			super.onClose(status);
			System.out.println("onClose");
			int i=0;
			//比對連線,刪除使用者唯一標識
			for(MessageInbound messageInbound : InitServlet.getSocketList()){
				if(this.equals(messageInbound)){
					userIds.remove(i);
					break;
				}
				i++;
				
			}
			InitServlet.getSocketList().remove(this);
		}

		@Override
		protected void onOpen(WsOutbound outbound) {
			System.out.println("onOpen");
			super.onOpen(outbound);
			InitServlet.getSocketList().add(this);
		}

		@Override
		protected void onBinaryMessage(ByteBuffer message) throws IOException {
			// TODO Auto-generated method stub
			System.out.println("onBinaryMessage");
		}

		@Override
		protected void onTextMessage(CharBuffer message) throws IOException {
			// TODO Auto-generated method stub
			System.out.println("onTextMessage=" + message);
			//獲取使用者id
			if (message.toString().indexOf("Hello, Server") > -1) {
				String idString = message.toString().split("%%%")[1];
				userIds.add(idString);
			}
			// this.getWsOutbound().writeTextMessage(CharBuffer.wrap("===="));
			// this.getWsOutbound().writeTextMessage(message);
			// 傳送給所有連結的
			// for (MessageInbound messageInbound : InitServlet.getSocketList())
			// {
			// CharBuffer buffer = CharBuffer.wrap(message);
			// WsOutbound outbound = messageInbound.getWsOutbound();
			// outbound.writeTextMessage(buffer);
			// outbound.flush();
			// }
			int i = 0;
			if (message.toString().indexOf("Hello, Server") <0)
				for (String item : userIds) {
					//根據使用者傳來id比對,確定傳送目標
					String str = message.toString().split("id")[1];
					if (item.equals(str)) {
						CharBuffer buffer = CharBuffer.wrap(message);
						WsOutbound outbound = InitServlet.getSocketList().get(i).getWsOutbound();
						outbound.writeTextMessage(buffer);
						outbound.flush();
					}
					i++;
				}
		}
	}
}

改一下儲存資料的結構,用list太麻煩,還是用map直接根據使用者id作為key,連結作為value,傳送的時候直接根據key獲取

Test

package com;

import java.io.IOException;
import java.nio.ByteBuffer;
import java.nio.CharBuffer;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;

import javax.servlet.http.HttpServletRequest;

import org.apache.catalina.websocket.MessageInbound;
import org.apache.catalina.websocket.StreamInbound;
import org.apache.catalina.websocket.WebSocketServlet;
import org.apache.catalina.websocket.WsOutbound;

public class Test extends WebSocketServlet {

	/**
* 
*/
	private static final long serialVersionUID = 1L;

	
	//儲存使用者和連結
	private static Map<String, MessageInbound> maps = new ConcurrentHashMap<String, MessageInbound>();

	@Override
	protected StreamInbound createWebSocketInbound(String subProtocol, HttpServletRequest request) {
		// TODO Auto-generated method stub
		return new WebSocketMessageInbound();
	}

	public class WebSocketMessageInbound extends MessageInbound {

		@Override
		protected void onClose(int status) {
			// InitServlet.getSocketList().remove(this);
			super.onClose(status);
			System.out.println("onClose");
		}

		@Override
		protected void onOpen(WsOutbound outbound) {
			System.out.println("onOpen");
			super.onOpen(outbound);
			InitServlet.getSocketList().add(this);
		}

		@Override
		protected void onBinaryMessage(ByteBuffer message) throws IOException {
			// TODO Auto-generated method stub
			System.out.println("onBinaryMessage");
		}

		@Override
		protected void onTextMessage(CharBuffer message) throws IOException {
			// TODO Auto-generated method stub
			System.out.println("onTextMessage=" + message);
			// 獲取使用者id
			if (message.toString().indexOf("Hello, Server") > -1) {
				String idString = message.toString().split("%%%")[1];
				maps.put(idString, InitServlet.getSocketList().get(0));
				InitServlet.getSocketList().remove(0);
			}
			if (message.toString().indexOf("Hello, Server") < 0) {
				//根據使用者id作為key獲取連結
				String str = message.toString().split("id")[1];
				CharBuffer buffer = CharBuffer.wrap(message);
				if (maps.get(str) != null) {
					WsOutbound outbound = maps.get(str).getWsOutbound();
					outbound.writeTextMessage(buffer);
					outbound.flush();
				}
			}
		}
	}
}