添加 socket 测试界面
This commit is contained in:
parent
03b4d85563
commit
9c169a0cf3
@ -17,10 +17,11 @@ struct Index {
|
|||||||
readonly itemGroups: HomeItemGroup[] = [
|
readonly itemGroups: HomeItemGroup[] = [
|
||||||
//Web部分
|
//Web部分
|
||||||
{
|
{
|
||||||
name: 'Web',
|
name: 'Net',
|
||||||
items: [
|
items: [
|
||||||
{ name: 'WebPage', page: 'pages/web/WebPage' },
|
{ name: 'WebPage', page: 'pages/web/WebPage' },
|
||||||
{ name: 'WebDialogPage', page: 'pages/web/WebDialogPage' }
|
{ name: 'WebDialogPage', page: 'pages/web/WebDialogPage' },
|
||||||
|
{ name: "TcpSocket", page: 'pages/net/TcpSocketPage'}
|
||||||
]
|
]
|
||||||
},
|
},
|
||||||
// 布局
|
// 布局
|
||||||
|
79
app/src/main/ets/pages/net/TcpSocketPage.ets
Normal file
79
app/src/main/ets/pages/net/TcpSocketPage.ets
Normal file
@ -0,0 +1,79 @@
|
|||||||
|
|
||||||
|
import socket from "@ohos.net.socket";
|
||||||
|
import { BusinessError } from '@ohos.base';
|
||||||
|
import { Log } from '@devwiki/base/Index';
|
||||||
|
|
||||||
|
class TcpSocket {
|
||||||
|
tcp: socket.TCPSocket = socket.constructTCPSocketInstance();
|
||||||
|
connectOptions: socket.TCPConnectOptions = {
|
||||||
|
address: {
|
||||||
|
address: '172.33.108.207',
|
||||||
|
port: 10000
|
||||||
|
},
|
||||||
|
timeout: 6000
|
||||||
|
}
|
||||||
|
|
||||||
|
constructor() {
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
init() {
|
||||||
|
let messageView = '';
|
||||||
|
this.tcp.on("message", (value: socket.SocketMessageInfo) =>{
|
||||||
|
for (let i: number = 0; i < value.message.byteLength; i++) {
|
||||||
|
let uint8Array = new Uint8Array(value.message)
|
||||||
|
let messages = uint8Array[i]
|
||||||
|
let message = String.fromCharCode(messages);
|
||||||
|
messageView += message;
|
||||||
|
}
|
||||||
|
Log.i(`receive message: ${messageView}`)
|
||||||
|
})
|
||||||
|
this.tcp.on('error', (error) => {
|
||||||
|
Log.i(`tcp error: ${error.message}`)
|
||||||
|
})
|
||||||
|
}
|
||||||
|
|
||||||
|
connect() {
|
||||||
|
let address: socket.NetAddress = {
|
||||||
|
address: '172.33.108.207',
|
||||||
|
}
|
||||||
|
this.tcp.bind(address)
|
||||||
|
this.tcp.connect(this.connectOptions, (error) => {
|
||||||
|
if (error) {
|
||||||
|
Log.e(`connect fail: ${error.message}`);
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
Log.i('connect success')
|
||||||
|
})
|
||||||
|
}
|
||||||
|
|
||||||
|
send(text: string) {
|
||||||
|
let option: socket.TCPSendOptions = { data: text}
|
||||||
|
this.tcp.send(option).then(() => {
|
||||||
|
Log.i(`send data: ${text} success`)
|
||||||
|
}).catch((error: BusinessError) => {
|
||||||
|
Log.e(`send data: ${text} error: ${error.message}`)
|
||||||
|
})
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
@Component
|
||||||
|
@Entry
|
||||||
|
export struct TcpSocketPage {
|
||||||
|
|
||||||
|
tcpSocket: TcpSocket = new TcpSocket();
|
||||||
|
|
||||||
|
aboutToAppear(): void {
|
||||||
|
this.tcpSocket.init();
|
||||||
|
this.tcpSocket.connect();
|
||||||
|
}
|
||||||
|
|
||||||
|
build() {
|
||||||
|
Column(){
|
||||||
|
Button("Send").width(80).height(32).onClick(() => {
|
||||||
|
this.tcpSocket.send("test")
|
||||||
|
})
|
||||||
|
}.width('100%').height('100%').justifyContent(FlexAlign.Center)
|
||||||
|
}
|
||||||
|
}
|
@ -7,6 +7,7 @@
|
|||||||
"pages/layout/RelativeContainerPage",
|
"pages/layout/RelativeContainerPage",
|
||||||
"pages/animation/CompTransitionPage",
|
"pages/animation/CompTransitionPage",
|
||||||
"pages/media/AVPlayerPage",
|
"pages/media/AVPlayerPage",
|
||||||
"pages/system/SchemePage"
|
"pages/system/SchemePage",
|
||||||
|
"pages/net/TcpSocketPage"
|
||||||
]
|
]
|
||||||
}
|
}
|
||||||
|
@ -3,7 +3,7 @@ import hilog from '@ohos.hilog';
|
|||||||
|
|
||||||
let domain: number = 0xFF00;
|
let domain: number = 0xFF00;
|
||||||
let prefix: string = 'HM4Demo';
|
let prefix: string = 'HM4Demo';
|
||||||
let format: string = `%{public}s, %{public}s`;
|
let format: string = `%{public}s`;
|
||||||
|
|
||||||
export class Log {
|
export class Log {
|
||||||
|
|
||||||
|
Loading…
Reference in New Issue
Block a user