/
autobahn.ets
102 lines (88 loc) · 2.76 KB
/
autobahn.ets
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
import * as autobahn from 'autobahn-browser'
@Entry
@Component
struct Autobahn {
@State subMsg: string= '';
@State appResult: string = '';
@State routerUrl: string = '';
@State connectState: string = 'Disconnected';
@State subTopic: string ='default';
@State pubTopic: string = 'default';
@State pubText: string = '';
connection: any = null;
build() {
Flex({ direction: FlexDirection.Column, alignItems: ItemAlign.Center, justifyContent: FlexAlign.Center }) {
Text(this.connectState).fontSize(25)
Text('Please input router url:').fontSize(20)
TextInput({
placeholder: "Please input router url...",
text: this.routerUrl
}).height(40)
.onChange((value: string) => {
this.routerUrl = 'ws://' + value;
})
Button('Connect to router')
.onClick(() => {
this.connection = new autobahn.Connection({ url: this.routerUrl+'/nightlife', realm: 'realm1' });
this.connection.onopen = ((session) => {
if (session.isOpen)
this.connectState='Connected';
})
this.connection.onclose = ((reason: string, details: object)=>{
console.log(reason);
this.connectState = 'Disconnected';
})
this.connection.open();
})
.fontSize(15)
.fontWeight(FontWeight.Bold)
Divider()
Button('Subscribe')
.onClick(()=>{
this.connection.session.subscribe(this.subTopic, (args)=> {
console.log("Event:", args[0]);
this.subMsg = args[0];
});
})
Text('Subscribed message: ' + this.subMsg)
.fontSize(20)
Divider()
Text('Publish Text')
.fontSize(15)
TextInput({
placeholder: "Please input text to publish...",
text: this.pubText
}).height(40)
.onChange((value: string) => {
this.pubText = value;
})
Button('Publish')
.onClick(()=>{
if(!(this.pubText===''))
this.connection.session.publish(this.pubTopic, [this.pubText]);
})
Divider()
Text('Press Register to set the add function').fontSize(15)
Button('Register')
.onClick(()=>{
function add2(args){
return (parseInt(args[0]) + parseInt(args[1])).toString();
}
this.connection.session.register('harmonyos.add2', add2);
})
Divider()
Button('Call(Add)')
.onClick(()=>{
this.connection.session.call('harmonyos.add2', [2, 3]).then(
(res) => {
console.log("Result:", res);
this.appResult = res;
});
})
Text('Application result: 2+3=' + this.appResult)
.fontSize(20)
}
.width('100%')
.height('100%')
}
}