Java 与 QtQuick 之 WebSocket
2016-05-03 13:28
483 查看
Java 与 QtQuick 之 WebSocket
WebSocket 是一种新的 HTML5 协议,它实现了浏览器与服务器全双工通信(full-duplex)。一开始的握手需要借助HTTP请求完成握手。直接上代码,Java 端:
package org.gdpurjyfs.websocket; import java.io.IOException; import java.util.concurrent.CopyOnWriteArraySet; import javax.websocket.CloseReason; 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 WebSocketServer { //静态变量,用来记录当前在线连接数。应该把它设计成线程安全的。 private static int onlineCount = 0; //concurrent包的线程安全Set,用来存放每个客户端对应的MyWebSocket对象。若要实现服务端与单一客户端通信的话,可以使用Map来存放,其中Key可以为用户标识 private static CopyOnWriteArraySet<WebSocketServer> webSocketSet = new CopyOnWriteArraySet<WebSocketServer>(); //与某个客户端的连接会话,需要通过它来给客户端发送数据 private Session session; /** * 连接建立成功调用的方法 * @param session 可选的参数。session为与某个客户端的连接会话,需要通过它来给客户端发送数据 */ @OnOpen public void onOpen(Session session){ this.session = session; webSocketSet.add(this); //加入set中 addOnlineCount(); //在线数加1 System.out.println("有新连接加入!当前在线人数为" + getOnlineCount()); } /** * 连接关闭调用的方法 */ @OnClose public void onClose(CloseReason reason){ webSocketSet.remove(this); //从set中删除 subOnlineCount(); //在线数减1 System.out.println("关闭原因是:"+reason.getReasonPhrase()); System.out.println("有一连接关闭!当前在线人数为" + getOnlineCount()); } /** * 收到客户端消息后调用的方法 * @param message 客户端发送过来的消息 * @param session 可选的参数 */ @OnMessage public void onMessage(String message, Session session) { System.out.println("来自客户端的消息:" + message); //群发消息 for(WebSocketServer item: webSocketSet){ try { item.sendMessage(item.session.getId()+": "+message); } catch (IOException e) { e.printStackTrace(); continue; } } } /** * 发生错误时调用 * @param session * @param error */ @OnError public void onError(Session session, Throwable error){ System.out.println("发生错误"); error.printStackTrace(); } /** * 这个方法与上面几个方法不一样。没有用注解,是根据自己需要添加的方法。 * @param message * @throws IOException */ public void sendMessage(String message) throws IOException{ this.session.getBasicRemote().sendText(message); //this.session.getAsyncRemote().sendText(message); } public static synchronized int getOnlineCount() { return onlineCount; } public static synchronized void addOnlineCount() { WebSocketServer.onlineCount++; } public static synchronized void subOnlineCount() { WebSocketServer.onlineCount--; } }
QtQuick 端:
import QtQuick 2.5 import QtQuick.Window 2.2 import QtQuick.Controls 1.4 import Qt.WebSockets 1.0 Window { visible: true WebSocket { id: webScoket url: "ws://localhost:8080/JavaWebSocket/websocket" active: true onStatusChanged: { console.log(status) } onTextMessageReceived: { console.log(message); } onErrorStringChanged: { console.log(errorString) } } Button { text: "send" onClicked: { webScoket.sendTextMessage("From QtQuick") console.log("send") } } }
相关文章推荐
- HDOJ3785 priority_queue AC, set TLE ,vector TLE
- MySQL java.sql.SQLException: Incorrect string value: '\xF0\x9F\xA4\x97' for column
- Maximum Subarray
- 子线程中更新UI
- Longest Common Prefix
- iOS对UIViewController生命周期和属性方法的解析(转载)
- MVC:从客户端中检测到有潜在危险的 Request.Form 值 的解决方法
- Bluetooth 蓝牙打开流程
- uitextfield placehold 颜色
- easyui 日期范围前后台的设置以及实现
- kendo ui grid 高度自适应
- 【Android UI】动态改变ListView布局
- CoreThink主题开发(七)使用H-ui开发博客主题之新闻资讯正文页面
- 知问前端——日历UI(三)
- easyUI获取index值以及修改某列的值
- 【Android UI】具有弹性的ListView
- ios上 更改 状态栏(UIStatusBar)的颜色
- 使用NSOperation以及NSOperationQueue
- UIView的autoresizingMask属性的研究
- UIBezierPath精讲