mzohaibnaz/neosocket
Composer 安装命令:
composer require mzohaibnaz/neosocket
包简介
Easy and simple library to create and manage web sockets.
README 文档
README
NeoSocket is a very simple and lite library that can help you to manage your socket logics. if you are using NeoSocket you should use NeoSocket - JS Client for complete solution package.
Installation
- Using composer
$ composer install mzohaibnaz/neosocket
How to use
- Initialization
- Setup socket
- Setup events
- Run Socket
- Sending data
- Get all clients
- TIP* : how to do code chain
- Dismiss client
Initializing NeoSocket
Initializing NeoSocket using SocketManager class
// Using neoSocket namespace use NeoSocket\SocketManager; // Initializing neoSocket with SocketManager $ns = new SocketManager();
Setup your socket
setup method will bind your socket on host localhost with port 6940
setup take 1 parameter as callback function
$ns->setup(function($socket){ // log on console socket is running print("\n\n Socket is Running \n\n"); });
Binding Socket to host/port
bind socket on different host and port using create method
create take 2 parameter as (host, port)
$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"); });
Setup events
After socket is setup now setup all your events inside it.
For setup events use on method.
on method take 2 parameter (event_name, callback_function)
$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); });
setup event with anonymous function
$socket->on("test", function($socket, $data){ // do something awesome here });
Default Event Types
NeoSocket library using 2 event types as default types to notify develop for new connection and disconnection of user.
connectionfor new connectiondisconnectedfor disconnection of user
New Connection
connection event-type take 2 parameters in callback
- socket reference
- uid of new connection
auto-generated
Example for new connection
// 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); });
On user disconnected
disconnected event-type take 2 parameters in callback
- socket reference
- uid of disconnected user
Example for disconnected
$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 socket after setup
run method is used to actually run your socket server after all events are setup.
Example
// 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
Data Sending
send method is used to send data on events.
send method take 1 parameter as data ( string / array )
$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"]); });
# send data to other event
event method used to select event-type before sending data on it.
event method take 1 parameter as event type
$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"); });
# send data to specific client
client method used to select client before sending data on it.
client method take 1 parameter as client 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"); });
Get all clients with attributes
getClients is used to get list of all active clients in socket with their attributes
$clients = $socket->getClients();
Set client custom attributes
addAttr will help you to add attributes to your client object for additional information storage.
addAttr take 2 parameters as key and value of an attribute.
Note: before adding attribute select client by client or clientByAttr method.
// for example you want to set first & last name for client $socket->client("uid")->addAttr("first","test")->addAttr("last","user");
Get client by attribute
clientByAttr is used to select client like client method but by its attribute value.
clientByAttr takes 3 parameters as mention below
keyin which you want to searchvaluevalue of that keyreferencevariable to store searched client .optional parameter
Note: if searched result is multiple code will select the very first matched client.
# Example Code
$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 Instance References
reset method is used to reset selected event/client for that current socket reference within on method
-
when you call event/client method to select that selective statement remain until you call reset method. this can help you to perform chain actions. like example below
-
Code Example
$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"); });
How to do code chain
$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 method used to disconnect client from socket.
dismiss method take 1 parameter as client 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();
License
mzohaibnaz/neosocket 适用场景与选型建议
mzohaibnaz/neosocket 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 4 次下载、GitHub Stars 达 1, 最近一次更新时间为 2019 年 11 月 02 日, 在 PHP 生态内属于活跃度较高的组件。
它主要适用于以下技术方向: 「sockets」 「WebSockets」 「Socket.io」 「simplesockets」 等业务场景。在实际项目中,围绕这些方向常见需要落地的问题包括:接口对接、性能调优、并发安全、与既有框架(Laravel / ThinkPHP / Yii / Webman 等)的兼容适配,以及生产环境的日志埋点与稳定性保障。
我们在过去多个企业项目中使用过 mzohaibnaz/neosocket 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。
基于 mzohaibnaz/neosocket 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。
线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。
承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。
与 mzohaibnaz/neosocket 相关的其它包
同方向 / 同关键字的高下载量 PHP Composer 包推荐,方便对比选型:
Synchronous Message Passing Framework for PHP
Thruway WAMP router core
A Laravel Nova tool to integrate websockets into Nova.
Binding for raw sockets (ext-sockets) in reactphp
NO LIBRARIES socket per page bridge for your Laravel application.
Laravel Video Chat using Socket.IO and WebRTC
统计信息
- 总下载量: 4
- 月度下载量: 0
- 日度下载量: 0
- 收藏数: 1
- 点击次数: 9
- 依赖项目数: 0
- 推荐数: 0
其他信息
- 授权协议: MIT
- 更新时间: 2019-11-02