前端代码
<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%> <!DOCTYPE html> <html> <head> <title>WebSoket Demo</title> <script type="text/JavaScript"> //验证浏览器是否支持WebSocket协议 if (!window.WebSocket) { alert("WebSocket not supported by this browser!"); } var ws; var log = function(s) { console.log("s"+s); if (document.readyState !== "complete") { console.log("s"+s); 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); } function getd() { //var valueLabel = document.getElementById("valueLabel"); //valueLabel.innerHTML = ""; ws=new WebSocket("ws://localhost:8080/WebSocketChat/websocket"); //监听消息 ws.onmessage = function(event) { //valueLabel.innerHTML+ = event.data; alert("a"); 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 }; } </script> </head> <body onload="getd()"> <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>
后端代码
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 javax.websocket.Session; 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 De 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) { //super.onOpen(outbound); System.out.println("打开"); } @Override protected void onBinaryMessage(ByteBuffer message) throws IOException { // TODO Auto-generated method stub System.out.println("...."); } @Override protected void onTextMessage(CharBuffer message) throws IOException { // TODO Auto-generated method stub System.out.println(message.toString()); } } }
使用到的jar包(catalina.jar,tomcat-coyote.jar)