php7 监听 scoket,socketio

phpsocket.io

A server side alternative implementation of socket.io in PHP based on Workerman.

Notice

Only support socket.io v1.3.0 or greater.

Namespace temporarily not implemented.

This porject is just trasnlate socket.io by workerman.

More api just see http://socket.io/docs/server-api/

Install

composer require workerman/phpsocket.io

Examples

Simple chat

use Workerman\Worker;

use PHPSocketIO\SocketIO;

// listen port 2021 for socket.io client

$io = new SocketIO(2021);

$io->on('connection', function($socket)use($io){

$socket->on('chat message', function($msg)use($io){

$io->emit('chat message', $msg);

});

});

Worker::runAll();

Another chat demo

use Workerman\Worker;

use PHPSocketIO\SocketIO;

// listen port 2020 for socket.io client

$io = new SocketIO(2020);

$io->on('connection', function($socket){

$socket->addedUser = false;

// when the client emits 'new message', this listens and executes

$socket->on('new message', function ($data)use($socket){

// we tell the client to execute 'new message'

$socket->broadcast->emit('new message', array(

'username'=> $socket->username,

'message'=> $data

));

});

// when the client emits 'add user', this listens and executes

$socket->on('add user', function ($username) use($socket){

global $usernames, $numUsers;

// we store the username in the socket session for this client

$socket->username = $username;

// add the client's username to the global list

$usernames[$username] = $username;

++$numUsers;

$socket->addedUser = true;

$socket->emit('login', array(

'numUsers' => $numUsers

));

// echo globally (all clients) that a person has connected

$socket->broadcast->emit('user joined', array(

'username' => $socket->username,

'numUsers' => $numUsers

));

});

// when the client emits 'typing', we broadcast it to others

$socket->on('typing', function () use($socket) {

$socket->broadcast->emit('typing', array(

'username' => $socket->username

));

});

// when the client emits 'stop typing', we broadcast it to others

$socket->on('stop typing', function () use($socket) {

$socket->broadcast->emit('stop typing', array(

'username' => $socket->username

));

});

// when the user disconnects.. perform this

$socket->on('disconnect', function () use($socket) {

global $usernames, $numUsers;

// remove the username from global usernames list

if($socket->addedUser) {

unset($usernames[$socket->username]);

--$numUsers;

// echo globally that this client has left

$socket->broadcast->emit('user left', array(

'username' => $socket->username,

'numUsers' => $numUsers

));

}

});

});

Worker::runAll();

Enable SSL for https

(phpsocket.io>=1.1.1 && workerman>=3.3.7 required)

require_once __DIR__ . '/vendor/autoload.php';

use Workerman\Worker;

use PHPSocketIO\SocketIO;

// ssl context

$context = array(

'ssl' => array(

'local_cert' => '/your/path/of/server.pem',

'local_pk' => '/your/path/of/server.key',

)

);

$io = new SocketIO(2021, $context);

$io->on('connection', function($connection)use($io){

echo "new connection coming\n";

});

Worker::runAll();

手册

Livedemo

Run chat example

cd examples/chat

Start

php server.php start for debug mode

php server.php start -d for daemon mode

Stop

php server.php stop

Status

php server.php status

License

MIT

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值