利用javax扩展包下的websocket API开发的websocket服务部署正常,代码如下:
构建maven工程,引入项目依赖jar包:
<dependency>
<groupId>javax.websocket</groupId>
<artifactId>javax.websocket-api</artifactId>
<version>1.1</version>
</dependency>
编写注解实现的WebSocketServer类:
package com.xxx.websocket;
import java.io.IOException;
import java.util.concurrent.CopyOnWriteArraySet;
import javax.websocket.OnClose;
import javax.websocket.OnError;
import javax.websocket.OnMessage;
import javax.websocket.OnOpen;
import javax.websocket.Session;
import javax.websocket.server.ServerEndpoint;
@ServerEndpoint("/websocket")
public class WebsocketService {
private static int online = 0;
private static CopyOnWriteArraySet<WebsocketService> websocketServices = new
CopyOnWriteArraySet<WebsocketService>();
private Session session;
@OnOpen
public void onOpen(Session session){
this.session = session;
websocketServices.add(this);
addOnlineCount();
System.out.println("有新的连接加入,当前在线人数为"+getOnlineCount());
}
@OnClose
public void onClose(){
websocketServices.remove(this);
subOnline();
System.out.println("有一个连接关闭,当前在线人数为"+getOnlineCount());
}
public static synchronized void subOnline() {
WebsocketService.online--;
}
public static synchronized int getOnlineCount() {
return online;
}
public static synchronized void addOnlineCount() {
WebsocketService.online++;
}
@OnMessage
public void onMessage(String message,Session session){
System.out.println("来自客户端的消息:"+message);
for(WebsocketService service:websocketServices){
try {
service.sendMessage(message);
} catch (Exception e) {
e.printStackTrace();
continue;
}
}
}
public void sendMessage(String message) throws IOException{
this.session.getBasicRemote().sendText(message);
}
@OnError
public void onError(Session session,Throwable e){
System.out.println("发生错误");
e.printStackTrace();
}
}
前端jsp页面:index.jsp
<%@page contentType="text/html;charset=UTF-8" %>
<!doctype html>
<html>
<head>
<title>websocket</title>
<script type="text/javascript">
var websocket = null;
if('WebSocket' in window){
websocket = new WebSocket("ws://10.119.9.167:8080/websocket");
}else{
alert("当前浏览器不支持WebSocket");
}
function send(){
var message = document.getElementById("message").value;
websocket.send(message);
}
function showMessage(message){
document.getElementById("message-list").innerHTML += message+"<br/>";
}
function closeWebsocket(){
websocket.close();
}
websocket.onerror = function(){
showMessage("连接发生错误");
}
websocket.onopen = function(){
showMessage("websocket连接成功");
}
websocket.onmessage = function(event){
showMessage(event.data);
}
websocket.onclose = function(){
showMessage("websocket连接关闭");
}
websocket.onbeforeupload = function(){
closeWebsocket();
}
</script>
</head>
<body>
<h2>chatroom with websocket</h2>
<div id="container">
<div>
<input type="text" name="message" id="message" placeholder="hello,websocket"/>
<input type="button" value="send" onclick="send()"/>
</div>
<div id="message-list">
</div>
</div>
</body>
</html>
启动tomcat,访问http://10.119.9.167:8080/
如题所示,tomcat8启动正常,访问页面也正常,当页面上的websocket连接tomcat部署的websocket服务器时,出现404错误:
出现这个问题,不是websocket服务器地址的问题,也不是tomcat7与tomcat8版本的问题。而是我们引入的依赖javax.websocket-api-1.1.jar和tomcat lib目录下自带的websocket-api.jar冲突了。
websocket的maven依赖
websocket在tomcat8中的依赖
其实没有必要引入这个javax.websocket-api-1.1.jar,但是如果不引入,项目中用到websocket api和注解的地方就会报错。我们可以将tomcat8作为server runtime library依赖加入到buildpath中。
只保留tomcat8中的websocket-api.jar,再次启动,访问http://10.119.9.167:8080一切正常。
分别开启一个chrome和一个firefox浏览器:
控制台打印的信息如下:
有新的连接加入,当前在线人数为1
有新的连接加入,当前在线人数为2
来自客户端的消息:hello,firefox
来自客户端的消息:hello,chrome
来自客户端的消息:hello,firefox2