2017 © Pedro Peláez
 

library websocket

a lightweight webSocket application library, server and client by sockets,streams,swoole

image

inhere/websocket

a lightweight webSocket application library, server and client by sockets,streams,swoole

  • Tuesday, March 27, 2018
  • by inhere
  • Repository
  • 1 Watchers
  • 3 Stars
  • 31 Installations
  • PHP
  • 0 Dependents
  • 0 Suggesters
  • 1 Forks
  • 0 Open issues
  • 2 Versions
  • 0 % Grown

The README.md

webSocket application

a lightweight webSocket application library, (*1)

install

in composer.json add:, (*2)

"inhere/websocket": "dev-master",

run: composer up, (*3)

usage

only use webSocket server

use Inhere\WebSocket\WebSocketServer;

$ws = new WebSocketServer();

$ws->on('open', function (WebSocketServer $ws, $data) {
    $ws->send('welcome!');
});

$ws->on(WebSocketServer::ON_MESSAGE, function (WebSocketServer $ws, $data) {
    $ws->send("you input: $data");
});

$ws->start();

use the webSocket application

use Inhere\WebSocket\Application;
use Inhere\WebSocket\WebSocketServer;

$app = new Application('', 9501);

$app->onOpen(function (WebSocketServer $ws, Application $app, $id) {
    $app->respond([
        'total' => $ws->count()
    ], 'welcome!');
});

$app->onClose(function (WebSocketServer $ws, Application $app) {
    $app->respond([
        'total' => $ws->count()
    ]);
});

$rootHandler = $app->route('/', new \Inhere\WebSocket\handlers\RootHandler());

// commands
$rootHandler->add('test', function ($data, $index, Application $app) {

    return 'hello';
});

webSocket header example

webSocket request header

// parsed code
GET / HTTP/1.1
Host: 127.0.0.1:9501
Connection: Upgrade
Pragma: no-cache
Cache-Control: no-cache
Upgrade: websocket
Origin: http://localhost:63342
Sec-WebSocket-Version: 13
User-Agent: Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/58.0.3018.3 Safari/537.36
Accept-Encoding: gzip, deflate, sdch, br
Accept-Language: zh-CN,zh;q=0.8
Cookie: _ga=GA1.1.1542925283.1469426767
Sec-WebSocket-Key: Tak3+4p37S5EAltSHDxpTw==
Sec-WebSocket-Extensions: permessage-deflate; client_max_window_bits


// source code
GET ws://127.0.0.1:9501/ HTTP/1.1
Host: 127.0.0.1:9501
Connection: Upgrade
Pragma: no-cache
Cache-Control: no-cache
Upgrade: websocket
Origin: http://localhost:63342
Sec-WebSocket-Version: 13
User-Agent: Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/58.0.3018.3 Safari/537.36
Accept-Encoding: gzip, deflate, sdch, br
Accept-Language: zh-CN,zh;q=0.8
Cookie: _ga=GA1.1.1542925283.1469426767
Sec-WebSocket-Key: BmMdv63hr1D/eS7eTD59Vw==
Sec-WebSocket-Extensions: permessage-deflate; client_max_window_bits

webSocket response header(handshake success)

// parsed code
Connection:Upgrade
Sec-WebSocket-Accept:BOVf/XCi92SSib4Ga+ltTsmHiWQ=
Upgrade:websocket

// source code
HTTP/1.1 101 Switching Protocol
Upgrade: websocket
Connection: Upgrade
Sec-WebSocket-Accept: BOVf/XCi92SSib4Ga+ltTsmHiWQ=

license

MIT, (*4)

The Versions

27/03 2018

dev-master

9999999-dev http://github.com/inhere/php-webSocket

a lightweight webSocket application library, server and client by sockets,streams,swoole

  Sources   Download

MIT

The Requires

 

The Development Requires

  • mylib/websocket-client dev-master

library websocket swoole sockets streams websocket-application

29/04 2017

0.5.1

0.5.1.0 http://github.com/inhere/php-webSocket

a lightweight webSocket application library, server and client by sockets,streams,swoole

  Sources   Download

MIT

The Requires

 

library websocket swoole sockets streams websocket-application