好文档 - 专业文书写作范文服务资料分享网站

Websocket使用

天下 分享 时间: 加入收藏 我要投稿 点赞

使用WebSocket推送服务器消息

1. 用户登录后通过js脚本创建websoket对象, 与服务器握手 2. 3. 4. 5. 6. var url = 'ws://localhost:8080/websocket/WebSocket?clientId='+clientId; CallCenter.init(url); var CallCenter = { init:function(url){ 7. var _websocket = new WebSocket(url); 8. 9. _websocket.onopen = function(evt) { 10. console.log(\ 11. }; 12. _websocket.onclose = function(evt) { 13. console.log(\ 14. }; 15. 16. //当登录者接受到新消息时,显示收件信息 17. _websocket.onmessage = function(evt) { 18. alert(evt.data); 19. }; 20. 21. _websocket.onerror = function(evt) { 22. console.log('Error occured: ' + evt); 23. }; 24. } 25. };

2. 创建对象

public class WebSocket extends WebSocketServlet {

private static final long serialVersionUID = 1L; /**

* @see HttpServlet#HttpServlet() */

public WebSocket() { super(); } /**

* 客户端连接后会调用此方法,创建StreamInbound对象 * */

@Override

protected StreamInbound createWebSocketInbound(String subProtocol, HttpServletRequest request) {

String clientId = request.getParameter(\ return new CallCenterMessageInBound(clientId); }

}

3. 将当前登录者信息列入socketList集合中, 创建添加、移除和发送方法

1. 2. 3. public class CallCenterMessageInBound extends MessageInbound { private String clientId; public static final Map socketList = new HashMap(); 4. 5. 6. 7. 8. 9. //构造方法 public CallCenterMessageInBound(String clientId) { this.clientId = clientId; } //发送方法(静态方法) public static void send(String clientId, String message) throws IOException { 10. MessageInbound messageInbound = socketList.get(clientId); 11. //只发送给socketList中存在的对象 12. //if(MessageInbound != null){ 13. messageInbound.getWsOutbound().writeTextMessage( CharBuffer.wrap(message)); 14. messageInbound.getWsOutbound().flush(); 15. //} 16. } 17. @Override 18. protected void onBinaryMessage(ByteBuffer message) throws IOException { 19. 20. } 21. @Override 22. protected void onTextMessage(CharBuffer message) throws IOException { 23. 24. } 25. //当客服端和服务器成功握手将调用这个方法,此方法将有效的webSocket对象存到socketList中 26. @Override 27. protected void onOpen(WsOutbound outbound) { 28. socketList.put(clientId, this); 29. } 30. //当客服端和服务连接断开的时候执行此方法,将移除无效的webSocket对象 31. @Override 32. protected void onClose(int status) { 33. socketList.remove(clientId); 34. } 35. } 36.

4. js调用发送方法,给指定的用户发送消息

1. 2. 3. 4. 5. 6. 7. 8. 9. $(document).ready(function(){ $(\ $.ajax({ type: \ url: \ data: \clientId=\#clientId\message=\#message\ success: function(msg){ alert(msg.message); } 10. }); 11. }); 12. }); 13. @Controller 14. public class MessageAPI { 15. /** 16. * 向客户端推送消息 17. * */ 18. @ResponseBody 19. @RequestMapping(value = \) 20. public Map send(@RequestParam String clientId, 21. @RequestParam String message) { 22. Map result = new HashMap(); 23. try { 24. CallCenterMessageInBound.send(clientId, message); 25. result.put(\, \); 26. } catch (IOException e) { 27. result.put(\, e.getLocalizedMessage()); 28. e.printStackTrace(); 29. } 30. return result;

Websocket使用

使用WebSocket推送服务器消息1.用户登录后通过js脚本创建websoket对象,与服务器握手2.3.4.5.6.varurl='ws://localhost:8080/websocket/WebSocket?clientId='+clientId;CallCenter.init(url);varCallCenter={
推荐度:
点击下载文档文档为doc格式
1n9z98teoa3y3j94w1iv
领取福利

微信扫码领取福利

微信扫码分享