Game socket
The library was published in early access and is not stable, as it is being developed in parallel with other solutions. English is not a native language so there are no comments. At this stage, the library is for those who want to understand the source code and get a starting point for their solution or help me :)
Features
- One library contains both Server and Client parts.
- The API communication library is similar to
Socket.io
, but not compatible with this solution. - Contains a built-in binary protocol so you don't have to work at the byte level.
- The transport layer uses
TCP
. To send game messages, it is planned to implement parallel work withUDP
. - It implements such concepts as Multiplexing - interaction with several spaces through a single channel.
Support for WebSocket
is not planned for the current day (but everything can change with the support of the community)
Example
Examples:
Usage
Create client:
This client connects to the main /
namespace on the server, then to the /home
namespace. Then it sends a request to enter the lobby
room, after which it dispatches a hello
event containing the message text hello all
.
Create server:
Server log
listen null Options{ port:3103 raw:true closeOnError:false }
/: connection GameClient{ 15466abe2006464e99b6c8b36f7f4ed8 ReadyState.open [137545126]}
/: createRoom 15466abe2006464e99b6c8b36f7f4ed8
/: joinRoom [15466abe2006464e99b6c8b36f7f4ed8, 15466abe2006464e99b6c8b36f7f4ed8]
Home: connect [/home, 15466abe2006464e99b6c8b36f7f4ed8]
Client log
open InternetAddress('127.0.0.1', IPv4) ReadyState.open
handshake Packet{[0.0 /], bit:516, bool:16, int:[0, 0, 60, 0, 0, 0], string:{3: 15466abe2006464e99b6c8b36f7f4ed8}}
>> Message{[/home] boolMask:4, int:[0, 0, 0, 0, 0, 0], string:{} null}
handshake Packet{[0.0 /home], bit:516, bool:16, int:[0, 0, 60, 0, 0, 0], string:{3: 15466abe2006464e99b6c8b36f7f4ed8}}
>> Message{[/home] boolMask:16, int:[0, 0, 0], string:{0: lobby} null}
>> Message{[/home] boolMask:512, int:[0, 0, 0], string:{0: lobby, 5: hello, 1: hello all} null}
Protocol
The protocol is schematic based. This approach allows you to save the amount of data transferred, since the data type is not transferred with the message, and the length of the numbers is not serialized.
Data types used in the schema
Type | Size | Range |
---|---|---|
bool | 1 bit | true or false |
int8 | 1 byte | 0 to 255 |
int16 | 2 bytes | 0 to 65535 |
int32 | 4 bytes | 0 to 4294967295 |
string | 1 + value | 0 to 255 chars |
bytes | 2 + value | 0 to 65535 bytes |
Schema creation
When you create a schema, you do two things: you take the named cell number of the array and determine the length of the array to one of the five schema data types.
Creating a message class
Data types when writing or reading messages
Operation | Schema Type | Dart Type | Range |
---|---|---|---|
putBool | bool | bool | true or false |
putInt | int8 | int | -128 to 127 |
putUInt | int8 | int | 0 to 255 |
putInt | int16 | int | -32768 to 32767 |
putUInt | int16 | int | 0 to 65535 |
putInt | int32 | int | -2147483648 to 2147483647 |
putUInt | int32 | int | 0 to 4294967295 |
putString | string | String | 0 to 255 chars |
putSingle | ~int8~ | double | 0 to 1 step ~0.004 |
putRadians | ~int16~ | double | step ~0.0002 |
putPayload | bytes | Uint8List | 0 to 65535 bytes |
Plans
- Initialization for sending
UPD
diagrams. - Automatic connections and reconnections.
- Expanding the possibilities for working with rooms.
- Conducting stress tests.
Tips for Beginners
- If you are developing a browser game, then you need a
WebSocket
solution. - When designing a game for real-time communication,
UDP
should be preferred, sinceTCP
will cause a delay in the event of packet loss.