php-websocket-server


WebSocket server for PHP

A WebSocket server for PHP, with a simple set of functions and callbacks for managing clients and the server.
Integer IDs are assigned for each client, making it very easy to store clients in arrays.





Check the Wiki for a list of functions, callbacks & settings.

The latest version is 0.2.
The WebSocket protocol currently supported is version 07, this works with Firefox 6.

An example chatbox is included in the download, showing how scripts can make use of the functions & callbacks.


0.2 Change Log

Performance Updates

Changed all "resource Socket" parameters to "integer ClientID".
This improves performance and makes it much easier to code with, eg. storing clients in arrays.

Changed all occurrences of $wsListen to $wsRead[0].

Changed sizeof($wsRead)-1 to global variable $wsClientCount to hold the amount of clients connected.

New Features

Added function wsGetClientSocket(integer ClientID)

Added setting WS_MAX_CLIENTS_PER_IP

Added "integer MessageLength" to callback wsOnMessage().
New syntax: wsOnMessage(integer ClientID, string Message, integer MessageLength, boolean Binary)

Added setting WS_MAX_FRAME_PAYLOAD_RECV, to limit the maximum payload data length of a frame.

Added setting WS_MAX_MESSAGE_PAYLOAD_RECV, to limit the maximum payload data length of a message.

Bug Fixes

Made wsStopServer() clear the $wsClients array.

When the max amount of clients is reached and a client connects, socket_accept() is now called, then socket_close() is called instantly after.

Fixed ping requests from being sent to clients after they connect and don't send any data, the server now waits WS_IDLE_LIMIT_READ seconds.

Fixed wsStartServer() to close the listen socket before returning false, if the server was unable to start.

When a frame is received from a client, the frame string length is now validated.
This fixed a php warning where the mask key bit was set, but the actual mask key was less than 4 bytes.

When a frame is received from a client with no payload data, the payload data decoding stage is now skipped.
This fixed the payload data from being read as 1 byte, and pong replies to a client now send no payload data back if none was received.

Clients in the connecting ready state that have not sent the opening handshake request, can now be timed out by the server, after WS_IDLE_LIMIT_READ seconds.

Made wsStopServer() close the TCP connection after sending a close frame, for all clients.

Made wsStopServer() only tell clients that the server is 'going away' if the opening handshake is complete.

Added frame buffering for incoming frames. Frames over 4096 bytes can now be read!

Fixed wsOnMessage() from being called with "boolean Binary" set to false, when a binary message is fragmented.

Fixed reading too many bytes into a frame if more bytes were received than the amount specified in the frame's payload length. The extra bytes are now parsed to the next frame.

If a non continuation frame is received from a client when there is data in the message buffer, the message buffer is now cleared before processing the frame.

Limited a frame's payload data length to 2,147,479,538 (2,147,483,647 - 14 - 4095).
The maximum integer in PHP is usually 2,147,483,647, then 14 bytes are taken for the frame's header length, and 4095 as a maximum for the last recv() call.

Limited a message's payload data length to 2,147,483,647. This is usually the maximum integer in PHP.

Changed the value of WS_STATUS_TIMEOUT from 1 to 3000, as range 0-999 is not used.

Prevented data from being sent to a client after a close frame has been sent to that client.

Small Changes

Changed WS_CLOSE_STATUS_TIMEOUT to WS_STATUS_TIMEOUT for consistency with other close status constant names.

Changed WS_IDLE_LIMIT_READ to WS_TIMEOUT_RECV, and WS_IDLE_LIMIT_PONG to WS_TIMEOUT_PONG

Project Information

The project was created on Aug 24, 2011.

  • License: GNU GPL v3
  • 15 stars
  • svn-based source control

Labels:
PHP Web Sockets Server