mzohaibnaz/neosocket

一个简单易用的库,用于创建和管理WebSocket。

1.0 2019-11-02 11:12 UTC

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();

许可

MIT