質問編集履歴

1

具体的に追記

2018/05/01 01:52

投稿

yamayamak
yamayamak

スコア131

test CHANGED
File without changes
test CHANGED
@@ -1,6 +1,30 @@
1
1
  Mosquitto-PHP(MQTTクライアント)にて、通信相手に送信(Publish)したら通信相手からのデータを受ける(subscribe/onMessage)プログラムを作ってます。
2
2
 
3
3
  送信した後に相手から受信が10秒間なかったらタイムアウトして切断(disconnect)したいですが、どのように記載すべきでしょうか?
4
+
5
+
6
+
7
+ 以下の動作は
8
+
9
+ (1)connectで接続
10
+
11
+ (2)subscribeでTopicを受信状態(onConnect内で接続されたらsubscribeしています。)
12
+
13
+ (3)publishでメッセージ送信(onSubscribe内でsubscribe完了したら送信(publish)しています。)
14
+
15
+ (4)subscribeしているTopicのメッセージを受信(onMessage内)
16
+
17
+ という感じで進めています。
18
+
19
+
20
+
21
+ (3)のpublishで通信相手(subscribe)に通信が送信しています。
22
+
23
+ (4)で通信相手がpublishしてくれたものをsubscribeで受ける処理を想定しています。
24
+
25
+ ただ、通信相手が確実に送信(Publish)してくれるかはわかりませんので10秒程度
26
+
27
+ onMessage(subscribeしているTopic)で待っているけど何も届かない場合にタイムアウトして切断(disconnect)したいと考えています。
4
28
 
5
29
 
6
30
 
@@ -28,7 +52,7 @@
28
52
 
29
53
  if ( $r == 0 ) {
30
54
 
31
- $c->subscribe('recv1', 0);
55
+ $c->subscribe('recv1', 0); // (2)
32
56
 
33
57
  } else {
34
58
 
@@ -48,13 +72,13 @@
48
72
 
49
73
  $jobType = 1;
50
74
 
51
- $mid = $c->publish('send1', "Hello job1", 0, false);
75
+ $mid = $c->publish('send1', "Hello job1", 0, false); // (3)
52
76
 
53
77
  echo "Sent1 message ID: {$mid}\n";
54
78
 
55
79
  });
56
80
 
57
- $c->onMessage(function($message) use ($c) {
81
+ $c->onMessage(function($message) use ($c) { // (4)
58
82
 
59
83
  printf("Got a message ID %d on topic %s with payload:\n%s\n\n", $message->mid, $message->topic, $message->payload);
60
84
 
@@ -92,7 +116,7 @@
92
116
 
93
117
 
94
118
 
95
- $c->connect("a.mosq.net", 1883, 60); // 60:KeepAlive
119
+ $c->connect("a.mosq.net", 1883, 60); // (1) 60:KeepAlive
96
120
 
97
121
  $c->loopForever();
98
122