-
Notifications
You must be signed in to change notification settings - Fork 15
/
helloworld.php
90 lines (81 loc) · 3.19 KB
/
helloworld.php
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
<?php
require __DIR__ . '/vendor/autoload.php';
use ExinOne\MixinSDK\Traits\MixinSDKTrait;
use ExinOne\MixinSDK\MixinSDK;
use Ramsey\Uuid\Uuid;
use Ratchet\RFC6455\Messaging\Frame;
$loop = \React\EventLoop\Factory::create();
$reactConnector = new \React\Socket\Connector($loop, [
'timeout' => 15
]);
$connector = new \Ratchet\Client\Connector($loop,$reactConnector);
class callTraitClass {
use MixinSDKTrait;
public $config;
public function __construct()
{
$config = require(__DIR__.'/config.php');
$this->config = $config;
}
}
$callTrait = new callTraitClass();
$Token = $callTrait->getToken('GET', '/', '');
// $connector('ws://127.0.0.1:9000', ['protocol' => 'Mixin-Blaze-1'], ['Origin' => 'http://localhost',
$connector('wss://blaze.mixin.one', ['protocol' => 'Mixin-Blaze-1'],[
'Authorization' => 'Bearer '.$Token
])
->then(function(Ratchet\Client\WebSocket $conn) {
$conn->on('message', function(\Ratchet\RFC6455\Messaging\MessageInterface $msg) use ($conn) {
$jsMsg = json_decode(gzdecode($msg));
print_r($jsMsg);
if ($jsMsg->action === 'CREATE_MESSAGE' and property_exists($jsMsg,'data')) {
echo "\nNeed reply server a receipt!\n";
$RspMsg = generateReceipt($jsMsg->data->message_id);
$msg = new Frame(gzencode(json_encode($RspMsg)),true,Frame::OP_BINARY);
$conn->send($msg);
if ($jsMsg->data->category === 'PLAIN_TEXT') {
$msgData = sendPlainText($jsMsg->data->conversation_id,
base64_decode($jsMsg->data->data));
$msg = new Frame(gzencode(json_encode($msgData)),true,Frame::OP_BINARY);
$conn->send($msg);
} //end of PLAIN_TEXT
} //end of CREATE_MESSAGE
});
$conn->on('close', function($code = null, $reason = null) {
echo "Connection closed ({$code} - {$reason})\n";
});
/* start listen for the incoming message */
$message = [
'id' => Uuid::uuid4()->toString(),
'action' => 'LIST_PENDING_MESSAGES',
];
print_r(json_encode($message));
$msg = new Frame(gzencode(json_encode($message)),true,Frame::OP_BINARY);
$conn->send($msg);
// $conn->send(gzencode($msg,1,FORCE_DEFLATE));
}, function(\Exception $e) use ($loop) {
echo "Could not connect: {$e->getMessage()}\n";
$loop->stop();
});
$loop->run();
function sendPlainText($conversation_id,$msgContent):Array {
$msgParams = [
'conversation_id' => $conversation_id,
'category' => 'PLAIN_TEXT',
'status' => 'SENT',
'message_id' => Uuid::uuid4()->toString(),
'data' => base64_encode($msgContent),//base64_encode("hello!"),
];
$msgPayButton = [
'id' => Uuid::uuid4()->toString(),
'action' => 'CREATE_MESSAGE',
'params' => $msgParams,
];
return $msgPayButton;
}
function generateReceipt($msgID):Array {
$IncomingMsg = ["message_id" => $msgID, "status" => "READ"];
$RspMsg = ["id" => Uuid::uuid4()->toString(), "action" => "ACKNOWLEDGE_MESSAGE_RECEIPT",
"params" => $IncomingMsg];
return $RspMsg;
}