websocket java服务端_H5 WebSocket java服务端push

1.pom

javax

javaee-api

7.0

这里使用javax.websocket,没有使用springmvc.websocket。

2.服务端 java代码

/***

* webScoket服务.

* format URL as ws://ip:port/{finalName}/websocket/{module}/{key}

*

*

* @author svili

* @data 2017年7月12日

*

*/

/**component注解是为了使用spring容器的依赖注入,以实现服务端push()*/

@Component

@ServerEndpoint(value = "/websocket/{module}/{key}")

public class SimpleWebSocket {

/** key = {module.key} */

private static ConcurrentHashMap> consumers = new ConcurrentHashMap>();

/**

* 连接建立成功调用的方法

*

* @param session

* session为与某个客户端的连接会话,需要通过它来给客户端发送数据

*/

@OnOpen

public void onOpen(@PathParam("module") String module, @PathParam("key") String key, Session session) {

if (!consumers.containsKey(module + "." + key)) {

Set group = new HashSet();

group.add(session);

consumers.put(module + "." + key, group);

} else {

consumers.get(module + "." + key).add(session);

}

}

@OnMessage

public void onMessage(@PathParam("module") String module, @PathParam("key") String key, String message,

Session session) throws IOException {

push(module, key, message);

}

@OnClose

public void onClose(@PathParam("module") String module, @PathParam("key") String key, Session session) {

Set group = consumers.get(module + "." + key);

if (group != null) {

group.remove(session);

}

}

@OnError

public void onError(@PathParam("module") String module, @PathParam("key") String key, Session session,

Throwable error) {

throw new RuntimeException(error);

}

/** 消息推送 */

public boolean push(String module, String key, String message) throws IOException {

Set group = consumers.get(module + "." + key);

if (group != null) {

for (Session consumer : group) {

consumer.getBasicRemote().sendText(message);

}

}

return true;

}

}

3.客户端(浏览器)JS代码

var websocket = null;

//判断当前浏览器是否支持WebSocket

if('WebSocket' in window){

websocket = new WebSocket("ws://ip:port/finalName/websocket/{module}/{key}");

}

else{

alert('Not support websocket');

}

//连接发生错误的回调方法

websocket.onerror = function(event){

alert("error");

};

//连接成功建立的回调方法

websocket.onopen = function(){

alert("open");

}

//接收到消息的回调方法

websocket.onmessage = function(event){

alert('recive : ' + event.data);

}

//连接关闭的回调方法

websocket.onclose = function(event){

alert("close");

}

//监听窗口关闭事件,当窗口关闭时,主动去关闭websocket连接,防止连接还没断开就关闭窗口,server端会抛异常。

window.onbeforeunload = function(){

websocket.close();

}

//发送消息

function send(message){

websocket.send(message);

}

4.服务端push

@RestController

@RequestMapping(value = "/websocket/test")

public class SocketTest {

@Resource

private SimpleWebSocket publisher;

@RequestMapping("/push")

public JsonModel push(String module, String key, String message) {

try {

publisher.push(module, key, message);

} catch (IOException e) {

e.printStackTrace();

}

//JsonModel是自定义的javaBean

return JsonModel.success("success");

}

}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值