在 WebSocket API,浏览器和服务器只需要要做一个握手的动作,然后,浏览器和服务器之间就形成了一条快速通道。两者之间就直接可以数据互相传送。下面小编给大家介绍Java中Websocket使用实例解析,欢迎阅读!
Java中Websocket使用实例解析运行环境
客户端
实现了websocket的浏览器
Chrome | Supported in version 4+ |
Firefox | Supported in version 4+ |
Internet Explorer | Supported in version 10+ |
Opera | Supported in version 10+ |
Safari | Supported in version 5+ |
服务端
依赖
<dependency>
<groupId>at</groupId>
<artifactId>tomcat-websocket-api</artifactId>
<version>7.0.47</version>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>javax</groupId>
<artifactId>javaee-api</artifactId>
<version>7.0</version>
<scope>provided</scope>
</dependency>
注意:早前业界没有统一的标准,各服务器都有各自的实现,现在J2EE7的JSR356已经定义了统一的标准,请尽量使用支持最新通用标准的服务器。
我是用的Tomcat 7.0.57 + Java7
必须是Tomcat 7.0.47以上
ps:最早我们是用的Tomcat 7自带的实现,后来要升级Tomcat 8,结果原来的实现方式在Tomcat 8不支持了,就只好切换到支持Websocket 1.0版本的Tomcat了。
主流的java web服务器都有支持JSR365标准的版本了,请自行Google。
用nginx做反向代理的需要注意啦,socket请求需要做特殊配置的,切记!
Tomcat的处理方式建议修改为NIO的方式,同时修改连接数到合适的参数,请自行Google!
服务端不需要在中做额外的配置,Tomcat启动后就可以直接连接了。
实现
import ionUtils;
import ;
import actory;
import ocket.*;
import Param;
import erEndpoint;
/**
* 功能说明:websocket处理类, 使用J2EE7的标准
* 切忌直接在该连接处理类中加入业务处理代码
*/
//relationId和userCode是我的业务标识参数,是连接的'路径,可以自行定义
@ServerEndpoint("/{relationId}/{userCode}")
public class WebsocketEndPoint {
private static Log log = og(s);
/**
* 打开连接时触发
* @param relationId
* @param userCode
* @param session
*/
@OnOpen
public void onOpen(@PathParam("relationId") String relationId,
@PathParam("userCode") int userCode,
Session session){
("Websocket Start Connecting: " + ey(relationId, userCode));
(relationId, userCode, session);
}
/**
* 收到客户端消息时触发
* @param relationId
* @param userCode
* @param message
* @return
*/
@OnMessage
public String onMessage(@PathParam("relationId") String relationId,
@PathParam("userCode") int userCode,
String message) {
return "Got your message (" + message + ")ks !";
}
/**
* 异常时触发
* @param relationId
* @param userCode
* @param session
*/
@OnError
public void onError(@PathParam("relationId") String relationId,
@PathParam("userCode") int userCode,
Throwable throwable,
Session session) {
("Websocket Connection Exception: " + ey(relationId, userCode));
(essage(), throwable);
ve(relationId, userCode);
}
/**
* 关闭连接时触发
* @param relationId
* @param userCode
* @param session
*/
@OnClose
public void onClose(@PathParam("relationId") String relationId,
@PathParam("userCode") int userCode,
Session session) {
("Websocket Close Connection: " + ey(relationId, userCode));
ve(relationId, userCode);
}
}
工具类用来存储唯一key和连接
这个是我业务的需要,我的业务是服务器有对应动作触发时,推送数据到客户端,没有接收客户端数据的操作。
import ion;
import ;
import urrentHashMap;
/**
* 功能说明:用来存储业务定义的sessionId和连接的对应关系
* 利用业务逻辑中组装的sessionId获取有效连接后进行后续操作
*/
public class SessionUtils {
public static Map clients = new ConcurrentHashMap<>();
public static void put(String relationId, int userCode, Session session){
(getKey(relationId, userCode), session);
}
public static Session get(String relationId, int userCode){
return (getKey(relationId, userCode));
}
public static void remove(String relationId, int userCode){
ve(getKey(relationId, userCode));
}
/**
* 判断是否有连接
* @param relationId
* @param userCode
* @return
*/
public static boolean hasConnection(String relationId, int userCode) {
return ainsKey(getKey(relationId, userCode));
}
/**
* 组装唯一识别的key
* @param relationId
* @param userCode
* @return
*/
public static String getKey(String relationId, int userCode) {
return relationId + "_" + userCode;
}
}
推送数据到客户端
在其他业务方法中调用
/**
* 将数据传回客户端
* 异步的方式
* @param relationId
* @param userCode
* @param message
*/
public void broadcast(String relationId, int userCode, String message) {
if (onnection(relationId, userCode)) {
(relationId, userCode)syncRemote()Text(message);
} else {
throw new NullPointerException(ey(relationId, userCode) + " Connection does not exist");
}
}
我是使用异步的方法推送数据,还有同步的方法
客户端代码
var webSocket = null;
var tryTime = 0;
$(function () {
initSocket();
foreunload = function () {
//离开页面时的其他操作
};
});
/**
* 初始化websocket,建立连接
*/
function initSocket() {
if (!ocket) {
alert("您的浏览器不支持websocket!");
return false;
}
webSocket = new WebSocket("ws://" + relationId + "/" + userCode);
// 收到服务端消息
ssage = function (msg) {
(msg);
};
// 异常
ror = function (event) {
(event);
};
// 建立连接
en = function (event) {
(event);
};
// 断线重连
ose = function () {
// 重试10次,每次之间间隔10秒
if (tryTime < 10) {
setTimeout(function () {
webSocket = null;
tryTime++;
initSocket();
}, 500);
} else {
tryTime = 0;
}
};
}
其他调试工具
Java实现一个websocket的客户端
依赖:
<dependency>
<groupId>-websocket</groupId>
<artifactId>Java-WebSocket</artifactId>
<version>1.3.0</version>
</dependency>
代码:
import ception;
import ntEndpoint;
import ror;
import ssage;
import en;
import ion;
@ClientEndpoint
public class MyClient {
@OnOpen
public void onOpen(Session session) {
tln("Connected to endpoint: " + asicRemote());
try {
asicRemote()Text("Hello");
} catch (IOException ex) {
}
}
@OnMessage
public void onMessage(String message) {
tln(message);
}
@OnError
public void onError(Throwable t) {
tStackTrace();
}
}
import eredReader;
import ception;
import tStreamReader;
import ;
import ainerProvider;
import oymentException;
import ion;
import ocketContainer;
public class MyClientApp {
public Session session;
protected void start()
{
WebSocketContainer container = ebSocketContainer();
String uri = "ws://";
tln("Connecting to " + uri);
try {
session = ectToServer(s, te(uri));
} catch (DeploymentException e) {
tStackTrace();
} catch (IOException e) {
tStackTrace();
}
}
public static void main(String args[]){
MyClientApp client = new MyClientApp();
t();
BufferedReader br = new BufferedReader(new InputStreamReader());
String input = "";
try {
do{
input = Line();
if(!ls("exit"))
asicRemote()Text(input);
}while(!ls("exit"));
} catch (IOException e) {
// TODO Auto-generated catch block
tStackTrace();
}
}
}
chrome安装一个websocket客户端调试
最后
为了统一的操作体验,对于一些不支持websocket的浏览器,请使用socketjs技术做客户端开发。