Download the PHP package mzohaibnaz/neosocket without Composer
On this page you can find all versions of the php package mzohaibnaz/neosocket. It is possible to download/install these versions without Composer. Possible dependencies are resolved automatically.
Download mzohaibnaz/neosocket
More information about mzohaibnaz/neosocket
Files in mzohaibnaz/neosocket
Package neosocket
Short Description Easy and simple library to create and manage web sockets.
License MIT
Homepage https://github.com/mzohaibnaz/neosocket
Informations about the package neosocket
NeoSocket - (PHP)
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
How to use
- Initialization
- Setup socket
-
- Change host & port
- Setup events
-
- Default event-types
-
- New Connection
-
- On Disconnect
- Run Socket
- Sending data
-
- Send to other event
-
- Send to client
- Get all clients
-
- Add attributes
-
- Get client by attribute
- TIP* : how to do code chain
- Dismiss client
Initializing NeoSocket
Initializing NeoSocket
using SocketManager
class
Setup your socket
setup
method will bind your socket on host localhost with port 6940
setup
take 1 parameter
as callback function
Binding Socket to host/port
bind socket on different host and port using create
method
create
take 2 parameter
as (host, port)
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)
setup event with anonymous function
Default Event Types
NeoSocket
library using 2 event types as default types to notify develop for new connection and disconnection of user.
connection
for new connectiondisconnected
for 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
On user disconnected
disconnected
event-type take 2 parameters in callback
- socket reference
- uid of disconnected user
Example for disconnected
Run socket after setup
run
method is used to actually run your socket server after all events are setup.
Example
Data Sending
send
method is used to send data on events.
send
method take 1 parameter
as data ( string / array
)
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
-
Example Code
-
Full code
send data to specific client
client
method used to select client
before sending data on it.
client
method take 1 parameter as client uid
-
Example Code
-
Full code
Get all clients with attributes
getClients
is used to get list of all active clients in socket with their attributes
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 clientByAttr method.
Example code
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
key
in which you want to searchvalue
value of that keyreference
variable to store searched client .optional parameter
Note: if searched result is multiple code will select the very first matched client.
Example Code
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
How to do code chain
Dismiss Client
dismiss
method used to disconnect client
from socket.
dismiss
method take 1 parameter as client uid
-
Example Code