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:
import 'package:game_socket/client.dart';
void main() {
var client = gameclientexample();
client.connect('localhost', 3103);
}
class gameclientexample extends gamesocketclient {
gameclientexample() {
on(event.handshake, (packet) => _onhandshake(packet));
on(event.roompacket, (packet) => _onroompacket(packet));
}
void _onhandshake(packet packet) {
if (packet.namespace == '/') {
sendmessage(connectrequest('/home'));
} else if (packet.namespace == '/home') {
sendmessage(joinroomrequest('lobby', namespace: '/home'));
}
}
void _onroompacket(roompacket packet) {
var roomname = packet.roomname;
if (packet.joinroom && roomname == 'lobby') {
var msg = roomevent(roomname!, namespace: '/home', event: 'hello', message: 'hello all');
sendmessage(msg);
}
}
}
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:
import 'package:game_socket/server.dart';
void main() {
var service = socketserviceexample();
service.listen();
}
class socketserviceexample {
late gamesocketserver server;
late namespace home;
socketserviceexample() {
server = gamesocketserver(options: serveroptions.bydefault()..supportrawdata = true);
home = server.of('/home');
home.on(serverevent.connect, (data) => _onhomeconnect(data));
home.on('hello', (packet) => _onhomedata(packet));
//
server.on(serverevent.connection, (socket) {
print('/: connection $socket');
socket.on(serverevent.connect, (data) => _onconnect(data[0], data[1]));
socket.on(event.disconnecting, (data) => _ondisconnecting(data));
socket.on(event.disconnect, (data) => _ondisconnect(data[0], data[1]));
socket.on(event.error, (data) => _onerror(data));
socket.on(event.data, (data) => _ondata(data));
socket.on(event.close, (data) => {_onclose(data)});
});
server.on(serverevent.error, (data) => {print('/: eventerror $data')});
server.on(serverevent.close, (data) => {print('/: serverclose $data')});
server.on(serverevent.raw, (data) => {print('/: raw $data')});
server.on(serverevent.createroom, (data) => {print('/: createroom $data')});
server.on(serverevent.joinroom, (data) => {print('/: joinroom $data')});
server.on(serverevent.leaveroom, (data) => {print('/: leaveroom $data')});
server.on(serverevent.deleteroom, (data) => {print('/: deleteroom $data')});
}
void listen() {
server.listen();
}
void _onhomeconnect(dynamic data) {
print('/home: connect $data');
}
void _onhomedata(dynamic data) {
print('/home: $data');
if (data is roompacket && data.roomname != null) {
home.broadcast(data, rooms: {data.roomname!});
}
}
void _onconnect(string namespace, string socketid) {
print('/: connect $socketid');
}
void _ondisconnecting(dynamic data) {
print('/: disconnecting $data');
}
void _ondisconnect(string namespace, string reason) {
print('$namespace: disconnect reason:$reason');
}
void _onerror(dynamic data) {
print('/: error $data');
}
void _ondata(dynamic data) {
print('/: $data');
}
void _onclose(dynamic data) {
print('/: close $data');
}
}
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
import 'package:game_socket/protocol.dart';
typedef ps = playerstateschema;
class playerstateschema extends simpleschema {
@override
int get code => 10; // unique schema code 10..255
@override
int get version => 1; // version 0..255 to support game clients with different versions
// bool
static const int reserved = 0; // reserved
@override
int get boolcount => 1;
// int8
static const int speed = 0; // 0.000..1.000
static const int health = 1; // max(100)
@override
int get int8count => 2;
// int16
static const int uid = 2; // max(65535)
static const int angle = 3; // radians
static const int score = 4; // max(65535)
@override
int get int16count => 3;
// int32
static const int elapsedtime = 5; // time for internal synchronization
static const int x = 6; // x-coordinate
static const int y = 7; // y-coordinate
@override
int get int32count => 3;
// strings
static const int name = 0; // player name
@override
int get stringscount => 1;
}
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
class playerstatemessage extends message {
playerstatemessage(player player, {required double elapsedtime}) : super(ps()) {
putuint(ps.uid, player.uid);
putint(ps.x, (player.positionbody.x * 1000).toint()); // ~ -2000000.0000..+2000000.0000
putint(ps.y, (player.positionbody.y * 1000).toint());
putint(ps.score, player.score);
putsingle(ps.speed, player.speed);
putradians(ps.angle, player.rotationbody);
putuint(ps.elapsedtime, (elapsedtime * 1000).toint()); // double ms
}
}
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.
Comments are closed.