Skip to content

Commit db97a0f

Browse files
committed
usage & refactory
1 parent 768e149 commit db97a0f

File tree

1 file changed

+166
-91
lines changed

1 file changed

+166
-91
lines changed

app.php

Lines changed: 166 additions & 91 deletions
Original file line numberDiff line numberDiff line change
@@ -1,99 +1,174 @@
11
<?php
22

3-
require __DIR__ . '/vendor/autoload.php';
4-
use ExinOne\MixinSDK\Traits\MixinSDKTrait;
5-
use Ramsey\Uuid\Uuid;
6-
use Ratchet\RFC6455\Messaging\Frame;
3+
require __DIR__ . '/vendor/autoload.php';
4+
use ExinOne\MixinSDK\Traits\MixinSDKTrait;
5+
use Ramsey\Uuid\Uuid;
6+
use Ratchet\RFC6455\Messaging\Frame;
77

8-
$loop = \React\EventLoop\Factory::create();
9-
$reactConnector = new \React\Socket\Connector($loop, [
10-
'dns' => '8.8.8.8',
11-
'timeout' => 15
12-
]);
13-
class callTraitClass {
14-
use MixinSDKTrait;
15-
public $config;
16-
public function __construct()
17-
{
18-
$config = require(__DIR__.'/config.php');
19-
$this->config = $config;
20-
}
21-
}
22-
$callTrait = new callTraitClass();
23-
$Token = $callTrait->getToken('GET', '/', '');
24-
print_r($callTrait->config['client_id']);
25-
// $Header = 'Authorization'.'Bearer '.$Token;
26-
// print($Header);
27-
$connector = new \Ratchet\Client\Connector($loop,$reactConnector);
28-
// $connector('ws://127.0.0.1:9000', ['protocol' => 'Mixin-Blaze-1'], ['Origin' => 'http://localhost',
29-
$connector('wss://blaze.mixin.one', ['protocol' => 'Mixin-Blaze-1'],[
30-
'Authorization' => 'Bearer '.$Token
31-
])
32-
->then(function(Ratchet\Client\WebSocket $conn) {
33-
$conn->on('message', function(\Ratchet\RFC6455\Messaging\MessageInterface $msg) use ($conn) {
34-
$jsMsg = json_decode(gzdecode($msg));
35-
print_r($jsMsg);
36-
if ($jsMsg->action === 'CREATE_MESSAGE' and property_exists($jsMsg,'data')) {
37-
echo "\nNeed reply server a receipt!\n";
38-
$IncomingMsg = ["message_id" => $jsMsg->data->message_id, "status" => "READ"];
39-
$RspMsg = ["id" => Uuid::uuid4()->toString(), "action" => "ACKNOWLEDGE_MESSAGE_RECEIPT",
40-
"params" => $IncomingMsg];
41-
$msg = new Frame(gzencode(json_encode($RspMsg)),true,Frame::OP_BINARY);
42-
$conn->send($msg);
8+
$loop = \React\EventLoop\Factory::create();
9+
$reactConnector = new \React\Socket\Connector($loop, [
10+
'dns' => '8.8.8.8',
11+
'timeout' => 15
12+
]);
13+
class callTraitClass {
14+
use MixinSDKTrait;
15+
public $config;
16+
public function __construct()
17+
{
18+
$config = require(__DIR__.'/config.php');
19+
$this->config = $config;
20+
}
21+
}
22+
$callTrait = new callTraitClass();
23+
$Token = $callTrait->getToken('GET', '/', '');
24+
print_r($callTrait->config['client_id']);
25+
// $Header = 'Authorization'.'Bearer '.$Token;
26+
// print($Header);
27+
$connector = new \Ratchet\Client\Connector($loop,$reactConnector);
28+
// $connector('ws://127.0.0.1:9000', ['protocol' => 'Mixin-Blaze-1'], ['Origin' => 'http://localhost',
29+
$connector('wss://blaze.mixin.one', ['protocol' => 'Mixin-Blaze-1'],[
30+
'Authorization' => 'Bearer '.$Token
31+
])
32+
->then(function(Ratchet\Client\WebSocket $conn) {
33+
$conn->on('message', function(\Ratchet\RFC6455\Messaging\MessageInterface $msg) use ($conn) {
34+
$jsMsg = json_decode(gzdecode($msg));
35+
print_r($jsMsg);
36+
if ($jsMsg->action === 'CREATE_MESSAGE' and property_exists($jsMsg,'data')) {
37+
echo "\nNeed reply server a receipt!\n";
38+
$IncomingMsg = ["message_id" => $jsMsg->data->message_id, "status" => "READ"];
39+
$RspMsg = ["id" => Uuid::uuid4()->toString(), "action" => "ACKNOWLEDGE_MESSAGE_RECEIPT",
40+
"params" => $IncomingMsg];
41+
$msg = new Frame(gzencode(json_encode($RspMsg)),true,Frame::OP_BINARY);
42+
$conn->send($msg);
4343

44-
if ($jsMsg->data->category === 'PLAIN_TEXT') {
45-
echo "PLAIN_TEXT:".base64_decode($jsMsg->data->data);
46-
$isCmd = strtolower(base64_decode($jsMsg->data->data));
47-
if ($isCmd = 'pay') {
48-
// print($callTrait->config['client_id']);
49-
$payLink = "https://mixin.one/pay?recipient=".
50-
"a1ce2967-a534-417d-bf12-c86571e4eefa"."&asset=".
51-
"6cfe566e-4aad-470b-8c9a-2fd35b49c68d".
52-
"&amount=0.0001"."&trace=".Uuid::uuid4()->toString().
53-
"&memo=";
54-
$msgData = [
55-
'icon_url' => "https://mixin.one/assets/98b586edb270556d1972112bd7985e9e.png",
56-
'title' => "Pay 0.001 EOS",
57-
'description' => "pay",
58-
'action' => $payLink,
59-
];
60-
$msgParams = [
61-
'conversation_id' => $jsMsg->data->conversation_id,// $callTrait->config[client_id],
62-
// 'recipient_id' => $jsMsg->data->user_id,
63-
'category' => 'APP_CARD',//'PLAIN_TEXT',
64-
'status' => 'SENT',
65-
'message_id' => Uuid::uuid4()->toString(),
66-
'data' => base64_encode(json_encode($msgData)),//base64_encode("hello!"),
67-
];
68-
$msgPayButton = [
69-
'id' => Uuid::uuid4()->toString(),
70-
'action' => 'CREATE_MESSAGE',
71-
'params' => $msgParams,
72-
];
73-
print_r($msgPayButton);
74-
print_r(json_encode($msgPayButton));
75-
$msg = new Frame(gzencode(json_encode($msgPayButton)),true,Frame::OP_BINARY);
76-
$conn->send($msg);
77-
}
44+
if ($jsMsg->data->category === 'PLAIN_TEXT') {
45+
echo "PLAIN_TEXT:".base64_decode($jsMsg->data->data);
46+
$isCmd = strtolower(base64_decode($jsMsg->data->data));
47+
if ($isCmd ==='?' or $isCmd ==='help') {
48+
$msgData = sendPlainText($jsMsg);
49+
$msg = new Frame(gzencode(json_encode($msgData)),true,Frame::OP_BINARY);
50+
$conn->send($msg);
7851
}
79-
}
80-
});
52+
if ($isCmd === '1') {
53+
// print($callTrait->config['client_id']);
54+
$msgData = sendAppButtons($jsMsg);
55+
$msg = new Frame(gzencode(json_encode($msgData)),true,Frame::OP_BINARY);
56+
$conn->send($msg);
57+
}//end of pay1
8158

82-
$conn->on('close', function($code = null, $reason = null) {
83-
echo "Connection closed ({$code} - {$reason})\n";
84-
});
85-
/* start listen for the incoming message */
86-
$message = [
87-
'id' => Uuid::uuid4()->toString(),
88-
'action' => 'LIST_PENDING_MESSAGES',
89-
];
90-
print_r(json_encode($message));
91-
$msg = new Frame(gzencode(json_encode($message)),true,Frame::OP_BINARY);
92-
$conn->send($msg);
93-
// $conn->send(gzencode($msg,1,FORCE_DEFLATE));
94-
}, function(\Exception $e) use ($loop) {
95-
echo "Could not connect: {$e->getMessage()}\n";
96-
$loop->stop();
59+
if ($isCmd === '2') {
60+
// print($callTrait->config['client_id']);
61+
$msgData = sendAppCard($jsMsg);
62+
$msg = new Frame(gzencode(json_encode($msgData)),true,Frame::OP_BINARY);
63+
$conn->send($msg);
64+
}//end of pay2
65+
}
66+
}
67+
});
68+
69+
$conn->on('close', function($code = null, $reason = null) {
70+
echo "Connection closed ({$code} - {$reason})\n";
9771
});
72+
/* start listen for the incoming message */
73+
$message = [
74+
'id' => Uuid::uuid4()->toString(),
75+
'action' => 'LIST_PENDING_MESSAGES',
76+
];
77+
print_r(json_encode($message));
78+
$msg = new Frame(gzencode(json_encode($message)),true,Frame::OP_BINARY);
79+
$conn->send($msg);
80+
// $conn->send(gzencode($msg,1,FORCE_DEFLATE));
81+
}, function(\Exception $e) use ($loop) {
82+
echo "Could not connect: {$e->getMessage()}\n";
83+
$loop->stop();
84+
});
85+
86+
$loop->run();
87+
88+
function sendPlainText($jsMsg):Array
89+
{
90+
$msgHelp = <<<HTML
91+
Usage:
92+
? or help : for help!
93+
1 : pay by APP_CARD
94+
2 : pay by APP_BUTTON_GROUP
95+
HTML;
96+
$msgParams = [
97+
'conversation_id' => $jsMsg->data->conversation_id,
98+
'category' => 'PLAIN_TEXT',
99+
'status' => 'SENT',
100+
'message_id' => Uuid::uuid4()->toString(),
101+
'data' => base64_encode($msgHelp),//base64_encode("hello!"),
102+
];
103+
$msgPayButton = [
104+
'id' => Uuid::uuid4()->toString(),
105+
'action' => 'CREATE_MESSAGE',
106+
'params' => $msgParams,
107+
];
108+
return $msgPayButton;
109+
}
110+
function sendAppButtons($jsMsg):Array {
111+
$payLinkEOS = "https://mixin.one/pay?recipient=".
112+
"a1ce2967-a534-417d-bf12-c86571e4eefa"."&asset=".
113+
"6cfe566e-4aad-470b-8c9a-2fd35b49c68d".
114+
"&amount=0.0001"."&trace=".Uuid::uuid4()->toString().
115+
"&memo=";
116+
$payLinkBTC = "https://mixin.one/pay?recipient=".
117+
"a1ce2967-a534-417d-bf12-c86571e4eefa"."&asset=".
118+
"c6d0c728-2624-429b-8e0d-d9d19b6592fa".
119+
"&amount=0.0001"."&trace=".Uuid::uuid4()->toString().
120+
"&memo=";
121+
$msgData = [[
122+
'label' => "Pay 0.001 EOS",
123+
'color' => "#FFABAB",
124+
'action' => $payLinkEOS,
125+
],[
126+
'label' => "Pay 0.0001 BTC",
127+
'color' => "#00EEFF",
128+
'action' => $payLinkBTC,
129+
],
130+
];
131+
$msgParams = [
132+
'conversation_id' => $jsMsg->data->conversation_id,// $callTrait->config[client_id],
133+
// 'recipient_id' => $jsMsg->data->user_id,
134+
'category' => 'APP_BUTTON_GROUP',//'PLAIN_TEXT',
135+
'status' => 'SENT',
136+
'message_id' => Uuid::uuid4()->toString(),
137+
'data' => base64_encode(json_encode($msgData)),//base64_encode("hello!"),
138+
];
139+
$msgPayButtons = [
140+
'id' => Uuid::uuid4()->toString(),
141+
'action' => 'CREATE_MESSAGE',
142+
'params' => $msgParams,
143+
];
144+
return $msgPayButtons;
145+
}
98146

99-
$loop->run();
147+
function sendAppCard($jsMsg):Array
148+
{
149+
$payLink = "https://mixin.one/pay?recipient=".
150+
"a1ce2967-a534-417d-bf12-c86571e4eefa"."&asset=".
151+
"6cfe566e-4aad-470b-8c9a-2fd35b49c68d".
152+
"&amount=0.0001"."&trace=".Uuid::uuid4()->toString().
153+
"&memo=";
154+
$msgData = [
155+
'icon_url' => "https://mixin.one/assets/98b586edb270556d1972112bd7985e9e.png",
156+
'title' => "Pay 0.001 EOS",
157+
'description' => "pay",
158+
'action' => $payLink,
159+
];
160+
$msgParams = [
161+
'conversation_id' => $jsMsg->data->conversation_id,// $callTrait->config[client_id],
162+
// 'recipient_id' => $jsMsg->data->user_id,
163+
'category' => 'APP_CARD',//'PLAIN_TEXT',
164+
'status' => 'SENT',
165+
'message_id' => Uuid::uuid4()->toString(),
166+
'data' => base64_encode(json_encode($msgData)),//base64_encode("hello!"),
167+
];
168+
$msgPayButton = [
169+
'id' => Uuid::uuid4()->toString(),
170+
'action' => 'CREATE_MESSAGE',
171+
'params' => $msgParams,
172+
];
173+
return $msgPayButton;
174+
}

0 commit comments

Comments
 (0)