在Ubuntu QML应用中使用WebSocket建立TCP/IP连接
来源:互联网 发布:mac word空白页删不掉 编辑:程序博客网 时间:2024/04/29 03:09
对于一些应用场景来说,TCP/IP连接是唯一的一种通讯的协议。对于我们的QML应用来说,我们可以使用WebSocket来建立一个双工的(full-duplex)的TCP/IP连接。在今天的例程中,我们将来介绍如何使用WebSocket来建立这种连接,并实现通信。
首先,我们得import我们需要的模块:
import Qt.WebSockets 1.0
然后,我们使用它:
import QtQuick 2.0import Ubuntu.Components 1.1import QtQuick.Layouts 1.1import Qt.WebSockets 1.0/*! \brief MainView with a Label and Button elements.*/MainView { // objectName for functional testing purposes (autopilot-qt5) objectName: "mainView" // Note! applicationName needs to match the "name" field of the click manifest applicationName: "websocket.liu-xiao-guo" /* This property enables the application to change orientation when the device is rotated. The default is false. */ //automaticOrientation: true // Removes the old toolbar and enables new features of the new header. useDeprecatedToolbar: false width: units.gu(60) height: units.gu(85) function appendMessage(msg) { var length = output.length; output.insert(length, msg + "\r\n"); } Page { id: page title: i18n.tr("websocket") WebSocket { id: socket url: input.text onTextMessageReceived: { console.log("something is received!"); appendMessage("received: " + message); } onStatusChanged: if (socket.status == WebSocket.Error) { console.log("Error: " + socket.errorString) } else if (socket.status == WebSocket.Open) { appendMessage("sending \"Hello world\""); socket.sendTextMessage("Hello World") } else if (socket.status == WebSocket.Closed) { appendMessage("Socket closed"); } active: true } Column { anchors.fill: parent spacing: units.gu(1) RowLayout { id: top width: parent.width TextField { id: input Layout.minimumWidth: page.width *.7 text: "ws://echo.websocket.org" } Button { id: get text: "Get" onClicked: { socket.active = true socket.sendTextMessage("Nice to meet you!") } } } TextArea { id: output width: parent.width height: page.height - top.height - units.gu(1) } } }}
在上面的代码中:
WebSocket {
id: socket
url: input.text
onTextMessageReceived: {
console.log("something is received!");
appendMessage("received: " + message);
}
onStatusChanged: if (socket.status == WebSocket.Error) {
console.log("Error: " + socket.errorString)
} else if (socket.status == WebSocket.Open) {
appendMessage("sending \"Hello world\"");
socket.sendTextMessage("Hello World")
} else if (socket.status == WebSocket.Closed) {
appendMessage("Socket closed");
}
active: true
}
我们从input.text中得到url。当active为真时,建立起Socket的连接。我们可以在onStatusChhanged中得到这个变化。当我们把active设为假时,安全套接字将被自动断开。在例程中,我们使用了一个公共的网站
ws://echo.websocket.org
每当我们向这个地址发送信息时,就会得到和发送信息一模一样的信息(是一个echo服务器)。我们可以通过onTextMessageReceived来得到这个信息。
运行我们的应用:
整个应用的源码在:https://github.com/liu-xiao-guo/websocket
0 0
- 在Ubuntu QML应用中使用WebSocket建立TCP/IP连接
- 如何在Ubuntu QML应用中使用Push Notification
- 正确地在QML应用中使用fontsize
- 正确地在QML应用中使用fontsize
- TCP/IP连接的建立
- TCP/IP连接的建立
- C#建立TCP/IP连接
- 怎么在Ubuntu QML应用中侦测到Swipe手势
- 如何在Ubuntu QML应用中播放音乐
- 如何在Ubuntu QML应用中播放视频
- 如何在Ubuntu QML应用中震动(vibration)
- 如何在Ubuntu QML应用中进行语言录音
- 如何在Ubuntu QML应用中实现MultipleSelectionListView
- 如何在Ubuntu QML应用中实现ComboBox的功能
- 在Ubuntu QML应用中实现Particle特效
- 在应用中集成TCP/IP
- 如何在QML应用中使用Javascript解析JSON
- 如何在QML应用中使用Javascript来解析XML
- Warning: bad syntax, perhaps a bogus '-'? See /usr/share/doc/procps-3.2.8/FAQ
- maya的面选择显示模式,中心点或整个面的设置方法
- HDU 2546饭卡 (01背包问题)
- (4.1.30)android 数据传递详解(Serialization、Parcelable、Parcel、Intent、Bundle)
- informatica session中bulk和normal模式
- 在Ubuntu QML应用中使用WebSocket建立TCP/IP连接
- 调用OpenSL ES NDK播放声音
- 阿里巴巴2015秋招算法类机试题目(三阶魔方)
- Android架构简介
- Limit JTextField input to a maximum length
- spring定时任务
- 【无限滚动加载数据】—infinite-scroll插件的使用---------下拉加载数据、无线滚动
- ural 1033. Labyrinth dfs
- Mysql (7)查询 删除重复