个人Websocket学习使用总结

主要参考了这篇文章:https://www.cnblogs.com/freud/p/8397934.html

1.客户端

var websocket = null;
var host = document.location.host;
var userid = "${user.id}"; // 获得当前登录人员的id
//判断当前浏览器是否支持WebSocket 
if ('WebSocket' in window) { 
    websocket = new WebSocket('ws://'+host+'/iats/webSocket/?userid='+userid); 
} else { 
    alert('当前浏览器不支持websocket') 
} 
			 
//连接发生错误的回调方法 
websocket.onerror = function() { 
    setMessageInnerHTML("WebSocket连接发生错误"); 
};  
			   
//连接成功建立的回调方法 
websocket.onopen = function() {
    setMessageInnerHTML("WebSocket连接成功"); 
} 
			   
//接收到消息的回调方法 
websocket.onmessage = function(event) {
    alert("接收到消息的回调方法") 
    alert("这是后台推送的消息:"+event.data);
    websocket.close();
    alert("webSocket已关闭!")
} 
			   
//连接关闭的回调方法 
websocket.onclose = function() { 
    setMessageInnerHTML("WebSocket连接关闭"); 
}
			   
//监听窗口关闭事件,当窗口关闭时,主动去关闭websocket连接,防止连接还没断开就关闭窗口,server端会抛异常。 
window.onbeforeunload = function() { 
    closeWebSocket(); 
} 
			   
//关闭WebSocket连接 
function closeWebSocket() { 
    websocket.close(); 
} 
			 
//将消息显示在网页上
function setMessageInnerHTML(innerHTML) {
    document.getElementById('message').innerHTML += innerHTML + '<br/>';
}

2.服务端

import java.io.IOException;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;

import javax.websocket.OnClose;
import javax.websocket.OnError;
import javax.websocket.OnMessage;
import javax.websocket.OnOpen;
import javax.websocket.Session;
import javax.websocket.server.PathParam;
import javax.websocket.server.ServerEndpoint;

import com.alibaba.fastjson.JSONObject;

@ServerEndpoint("/webSocket/")  
public class WebSocket { 
    private static int onlineCount = 0; 
    private static Map<String, WebSocket> clients = new ConcurrentHashMap<String, WebSocket>(); 
    private Session session; 
    private String userid; 
       
    @OnOpen 
    public void onOpen(@PathParam("userid") String userid, Session session) throws IOException { 
   
        this.userid = userid; 
        this.session = session; 
           
        addOnlineCount(); 
        clients.put(userid, this);
        System.out.println("已连接");
    } 
   
    @OnClose 
    public void onClose() throws IOException { 
        clients.remove(userid); 
        subOnlineCount(); 
    } 
   
    @OnMessage 
    public void onMessage(String message) throws IOException { 
   
        JSONObject jsonTo = JSONObject.parseObject(message);
        String mes = (String) jsonTo.get("message");
         
        if (!jsonTo.get("To").equals("All")){ 
            sendMessageTo(mes, jsonTo.get("To").toString()); 
        }else{ 
            sendMessageAll("给所有人"); 
        } 
    } 
   
    @OnError 
    public void onError(Session session, Throwable error) { 
        error.printStackTrace(); 
    } 
   
    public void sendMessageTo(String message, String To) throws IOException { 
        // session.getBasicRemote().sendText(message); 
        //session.getAsyncRemote().sendText(message); 
        for (WebSocket item : clients.values()) { 
            if (item.userid.equals(To) ) 
                item.session.getAsyncRemote().sendText(message); 
        } 
    } 
       
    public void sendMessageAll(String message) throws IOException { 
        for (WebSocket item : clients.values()) { 
            item.session.getAsyncRemote().sendText(message); 
        } 
    } 
   
    public static synchronized int getOnlineCount() { 
        return onlineCount; 
    } 
   
    public static synchronized void addOnlineCount() { 
        WebSocket.onlineCount++; 
    } 
   
    public static synchronized void subOnlineCount() { 
        WebSocket.onlineCount--; 
    } 
   
    public static synchronized Map<String, WebSocket> getClients() { 
        return clients; 
    } 

3.调用

WebSocket ws = new WebSocket();
JSONObject jo = new JSONObject();
jo.put("message", "这是后台返回的消息!");
jo.put("To", UserUtils.getUser().getId());
ws.onMessage(jo.toString());

4.遇到的问题

  • 连接失败,报404错误:WebSocket connection to 'ws://localhost:8080/iats/websocket/?userid=1' failed: Error during WebSocket handshake: Unexpected response code: 404

    web容器换成tomcat就行了,参考文章:https://www.oschina.net/question/1756518_238747

  • 服务端接收不到客户端传过来的参数,传过来的userid为null

    本来以为websocket可以和http协议一样传参,于是这样写了:

websocket = new WebSocket('ws://'+host+'/iats/webSocket/?userid='+userid); 

    然而不能这样传,改成这样就行了

    客户端:

websocket = new WebSocket('ws://'+host+'/iats/webSocket/'+userid); 

    服务端:

@ServerEndpoint("/webSocket/{userid}")  

    传多个参:

websocket = new WebSocket('ws://'+host+'/iats/webSocket/'+userid+'/'+param); 
@ServerEndpoint("/webSocket/{userid}/{param}")  
public void onOpen(@PathParam("userid") String userid, @PathParam("param") String param, Session session) throws IOException {



  • 推送时后台报错:The remote endpoint was in state [TEXT_FULL_WRITING] which is an invalid state for called method

    产生原因貌似是:发送数据频繁而导致发送重叠。 我的websocket使用场景是自动化测试中,对代码进行分析,将分析日志同步推送到页面展示。所以确实会每秒推送大量信息。

    解决方案:

item.session.getAsyncRemote().sendText(message); 

改为

item.session.getBasicRemote().sendText(message); 
AsyncRemote是异步推送,BasicRemote为同步推送,同步推送中,前面一条没推送完时,会阻塞推送。一般使用情况下,比如做的是页面聊天系统之类的,推荐用异步推送。然而我的运用场景反而用同步推送比较适合,所以改为BasicRemote就不报错了
  • 0
    点赞
  • 8
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值