Crystal Byte Protocol

CI Documentation ISC

Installation

Add the dependency to your shard.yml:

dependencies:
  crystal-byte-protocol:
    github: j8r/crystal-byte-protocol

Documentation

https://j8r.github.io/crystal-byte-protocol

Usage

See the specs to have concrete examples.

Generate JS protocol messages

In a bin/gen-js-protocol-messages.cr file:

require "crystal-byte-protocol/crystal_to_js_byte_format"
require "../src/protocol.cr"

dir = Path.new "../web-client/js/protocol"

CrystalByteProtocol::CrystalToJS.convert_to_file(
  dir / "Client.js",
  MyApp::Protocol::Client
)

CrystalByteProtocol::CrystalToJS.convert_to_file(
  dir / "Server.js",
  MyApp::Protocol::Server
)

Use protocol messages in JS with WebSocket

Be sure to set the WebSocket binaryType to "arraybuffer"

const socket = new WebSocket(address)
socket.binaryType = "arraybuffer"

this.socket.onmessage = (event) => {
  const message = Server.deserialize(new ByteDecoder(event.data))
  console.log(message)
}

const object = new Client.MyObject
socket.send(Client.serialize(new ByteEncoder(), object))

License

Copyright (c) 2021-2023 Julien Reichardt - ISC License