mzohaibnaz / neosocket
一个简单易用的库,用于创建和管理WebSocket。
Requires
- php: >=7.1.7
This package is auto-updated.
Last update: 2024-09-29 05:28:54 UTC
README
NeoSocket是一个非常简单且轻量级的库,可以帮助您管理您的套接字逻辑。如果您使用NeoSocket,应该使用NeoSocket - JS Client以获得完整的解决方案包。
安装
- 使用composer
$ composer install mzohaibnaz/neosocket
如何使用
初始化NeoSocket
使用SocketManager
类初始化NeoSocket
// Using neoSocket namespace use NeoSocket\SocketManager; // Initializing neoSocket with SocketManager $ns = new SocketManager();
设置您的套接字
setup
方法将您的套接字绑定到localhost的主机,端口为6940。 setup
方法接受1个参数
作为回调函数
$ns->setup(function($socket){ // log on console socket is running print("\n\n Socket is Running \n\n"); });
将套接字绑定到主机/端口
使用create
方法在不同的主机和端口上绑定套接字。create
方法接受2个参数
作为(主机,端口)
$ns->create("localhost", 1414)->setup(function($socket){ // log on console socket is running print("\n\n Socket is Running on localhost with port 1414 \n\n"); });
设置事件
在套接字设置完成后,现在设置它内部的所有事件。要设置事件,请使用on
方法。on
方法接受2个参数
(事件名称,回调函数)
$ns->setup(function($socket){ // event setup callback take 2 parameter // first socket reference. second contain data from the client for that event function callback_fnc($socket, $data){ // do something awesome here }; $socket->on("test", callback_fnc); });
使用匿名函数设置事件
$socket->on("test", function($socket, $data){ // do something awesome here });
默认事件类型
NeoSocket
库使用2种事件类型作为默认类型,以通知开发人员新连接和用户断开连接。
connection
用于新连接disconnected
用于用户断开连接
新连接
connection
事件类型在回调中接受2个参数
- 套接字引用
- 新连接的uid
自动生成
新连接示例
// default event whenever there is new connection $socket->on("connection", function($socket, $uid){ // log on console about new user with unique id // $uid is a unique id for each user in socket print("\n new user is here with id: {$uid}"); // tell other users that new user is here with event `newuser` // that will send data to javascript client of neoSocket with event `newuser` // for chat room example tell all other users that, // there is new user $socket->event("newUser")->send("new user with id! : ".$uid); });
用户断开连接时
disconnected
事件类型在回调中接受2个参数
- 套接字引用
- 断开连接用户的uid
断开连接示例
$socket->on("disconnected", function($socket, $uid){ echo "\n user disconnected : {$uid} \n"; // for chat room example tell other user that user is disconnected $socket->event("ondisconnect")->send("\n disconnected user with id! : {$uid} \n"); });
设置后运行套接字
run
方法用于在所有事件设置后实际运行您的套接字服务器。
示例
// Example #1 $ns->setup(function($socket){ // setup socket here })->run(); // Example #2 $myserver = $ns->setup(function($socket){ // setup socket here }); $myserver->run(); // run socket to accept new connections
数据发送
send
方法用于在事件上发送数据。send
方法接受1个参数
作为数据(字符串/数组
)
$socket->on("test", function($socket, $data){ // send data to `test` event $socket->send("hello test"); // send data as array to `test` event $socket->send(["hello","world","test"]); });
# 发送数据到其他事件
event
方法用于在发送数据之前选择事件类型
。event
方法接受1个参数作为事件类型
$socket->event("neo")->send("hello neo");
$socket->on("test", function($socket, $data){ // send data to `test` event $socket->send("hello test"); // send data to event type `neo` $socket->event("neo")->send("hello neo"); });
# 发送数据到特定客户端
client
方法用于在发送数据之前选择客户端
。client
方法接受1个参数作为客户端 uid
$socket->client("testclient")->send("hello test user");
$socket->on("test", function($socket, $data){ // send data to `test` event $socket->send("hello test event"); // send data to only `testclient` $socket->client("testclient")->send("hello test user"); });
获取所有带有属性的客户端
getClients
用于获取套接字中所有活动客户端及其属性列表
$clients = $socket->getClients();
设置客户端自定义属性
addAttr
可以帮助您向客户端对象添加属性以存储附加信息。addAttr
接受2个参数作为属性的键
和值
。
// for example you want to set first & last name for client $socket->client("uid")->addAttr("first","test")->addAttr("last","user");
通过属性获取客户端
clientByAttr
用于选择客户端,类似于使用 client 方法
,但通过其属性值。 clientByAttr
接受以下 3 个参数
key
想要搜索的键value
该键的值reference
用于存储搜索到的客户端的变量。可选参数
注意:如果搜索结果有多个,代码将选择第一个匹配的客户端。
# 示例代码
$socket->on("test", function($socket, $data){ $found_client = false; // store selected client in found_client varible // and send message to selected client $socket->clientByAttr("username","test",$found_client)->send("hello test user"); });
重置实例引用
reset
方法用于在 on
方法中重置当前套接字引用所选的事件/客户端
$socket->on("test", function($socket, $data){ // this line send data to event-type `test` $socket->send("send data to test event"); // this line send data to event-type `neo` $socket->event("neo")->send("send data to neo event"); // because neo is still selected, // this line will send data to neo event-type $socket->send("send data to neo event"); // reset references $socket->reset(); // because all selective statements are reset, // now send will send data to `test` event-type, // because you are calling send method in `test`event $socket->send("hello test event-type"); });
如何进行代码链操作
$socket->on("test", function($socket, $data){ // simple example of code chain :) $socket->send("send data to test event") ->event("neo")->send("send data to neo event"); ->send("send data to neo event because `neo` event is selected"); ->client("testuser") ->send("sending data to `testuser` with event-type `neo`") ->event("greating") ->send("sendinig data to `testuser but now with event-type `greating`") ->reset() ->send("send data to event-type `test` because references are reset!"); });
移除客户端
dismiss
方法用于将 client
从套接字断开连接。 dismiss
方法接受一个参数:客户端的 uid
// dismiss client from socket with uid $socket->dismiss("testclient"); // select client then dismiss it $socket->client($uid)->dismiss(); // select client by attribute and dismiss it $socket->clientByAttr("username", "test")->dismiss();