123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117 |
- <?php
- namespace app\act\socket;
- use think\worker\Server;
- class Worker extends Server
- {
- protected $clients = array();
- protected $socket = 'websocket://0.0.0.0:2346';
-
- public function onMessage($connection, $data)
- {
-
-
- parse_str($data, $data);
-
- if (!isset($data['signature']) && !isset($data['to']) && !isset($data['msg'])) {
-
- $connection->send('status=2&msg=error');
- } else {
- $data['user'] = $this->load('api/lib/user')->decode($data['signature']);
- if (!$data['user']) {
- $connection->send('status=2&msg=error');
- }
- if (isset($data['user']['uid']) && $data['user']['uid'] > 0 && $data['user']['uid'] != $data['to']) {
-
-
- if (!isset($this->clients[$data['user']['uid']])) {
-
- $ip = $connection->getRemoteIp();
- $port = $connection->getRemotePort();
- $this->clients[$data['user']['uid']]] = array
- (
- 'ip' => $ip,
- 'port' => $port,
- 'connection' => $connection
- );
- }
-
-
- if (isset($this->clients[$data['to']])) {
-
- $this->clients[$data['to']]['connection']->send($data['msg']);
- } else {
-
- }
- } else {
- $connection->send('status=2&msg=error');
- }
- }
- }
-
- public function onConnect($connection)
- {
- }
-
- public function onClose($connection)
- {
- unset($this->clients[$connection->getRemoteIp()]);
- }
-
- public function onError($connection, $code, $msg)
- {
- echo "error $code $msg\n";
- }
-
- public function onWorkerStart($worker)
- {
- }
- }
|