Download the PHP package nusje2000/websockets without Composer
On this page you can find all versions of the php package nusje2000/websockets. It is possible to download/install these versions without Composer. Possible dependencies are resolved automatically.
Informations about the package websockets
Websocket server
Almost fully compatible with https://tools.ietf.org/html/rfc6455
Simple version can be found here: https://developer.mozilla.org/en-US/docs/Web/API/WebSockets_API/Writing_WebSocket_servers
Data framing
The following data frame was used to parse messages from and to the clients.
Opcodes
hex | int | text | const | control |
---|---|---|---|---|
0x0 | 0 | Continuation | OpcodeEnum::CONTINUE | true |
0x1 | 1 | Text | OpcodeEnum::TEXT | false |
0x2 | 2 | Binary | OpcodeEnum::BIN | false |
0x3 | 3 | No meaning | OpcodeEnum::OPCODE_3 | false |
0x4 | 4 | No meaning | OpcodeEnum::OPCODE_4 | false |
0x5 | 5 | No meaning | OpcodeEnum::OPCODE_5 | false |
0x6 | 6 | No meaning | OpcodeEnum::OPCODE_6 | false |
0x7 | 7 | No meaning | OpcodeEnum::OPCODE_7 | false |
0x8 | 8 | Close | OpcodeEnum::CLOSE | true |
0x9 | 9 | Ping | OpcodeEnum::PING | true |
0xA | 10 | Pong | OpcodeEnum::PONG | true |
0xB | 11 | No meaning | OpcodeEnum::OPCODE_11 | true |
0xC | 12 | No meaning | OpcodeEnum::OPCODE_12 | true |
0xD | 13 | No meaning | OpcodeEnum::OPCODE_13 | true |
0xE | 14 | No meaning | OpcodeEnum::OPCODE_14 | true |
0xF | 15 | No meaning | OpcodeEnum::OPCODE_15 | true |
Mapping
All incomming frames are being mapped to the Frame class, this class is basically an oop version of the dataframe displayed above. The following mapping is used:
FIN
=> Frame::isFinal(): boolRSV1
=> noneRSV2
=> noneRSV3
=> noneOPCODE
=> Frame::getOpcode(): intMASK
=> Frame::isMasked(): boolPAYLOAD_LENGTH
=> Frame::getPayloadLength(): intMASKING_KEY
=> Frame::getMaskingKey(): stringPAYLOAD_DATA
=> Frame::getPayload(): string
Additional functionallity
There are a few functions added to the Frame for ease of use:
- Frame::isClosing(): bool => is true when opcode =
0x8
- Frame::isControl(): bool => is true when opcode is for contol
- Frame::isNonControl(): bool => is true when opcode is not for contol
Events
The websocket makes use of the symfony event dispatcher. Because of this, each incomming dataframe wil dispatch an event. This event can be used to react on incomming frames. The following is a list of events that can be listened to:
Event class | Reason |
---|---|
HandshakeEvent | first data sent over a new connection will be treated as handshake request |
ConnectEvent | new connection (handshake is already done when this event is triggered) |
DataEvent | received data (triggered when data is received) |
FrameEvent | received frame (triggered when a frame is received) |
MessageEvent | received message (only works when the FrameEventSubscriber is an active listener, triggered when a frame is received with TEXT opcode) |
DisconnectEvent | disconnected (triggered when a connection is disconned) |
For all events to be dispatched there are 4 event dispatchers. The following is a list of the dispatchters and their in-/outgoning events:
Dispatcher | Incomming events | Outgoing events | Purpose |
---|---|---|---|
HandshakeEventSubscriber | HandshakeEvent | ConnectEvent | Handle handshake and dispatch a connect event when successfull |
ConnectionEventSubscriber | ConnectEvent, DisconnectEvent | DataEvent, DisconnectEvent | Handle connection and map connection events |
DataEventSubscriber | DataEvent | FrameEvent | Parse incomming data to a frame and dispatch as frame event |
FrameEventSubscriber | FrameEvent | MessageEvent | Handle incomming frame event and dispatch a Message event if the opcode is TEXT |
Running the example
There is a simple chat application included in this project. To run this, use the following commands:
You must run both commands separate from each other
Known issues
- Fragmentations is not supported yet
- Encode function of the encoder does not yet support masking
- Strings longer than 65536 cannot be received due to buffering
All versions of websockets with dependencies
symfony/event-dispatcher Version ^3.4 || ^4.0
psr/log Version ^1.1
aeviiq/collection Version ^2.0
myclabs/php-enum Version ^1.7