crystal-byte-protocol

A byte protocol for Crystal

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

Repository

crystal-byte-protocol

Owner
Statistic
  • 4
  • 0
  • 0
  • 0
  • 1
  • almost 2 years ago
  • January 11, 2021
License

ISC License

Links
Synced at

Thu, 21 Nov 2024 16:35:45 GMT

Languages