https://github.com/j8r/crystal-byte-protocol
A byte protocol for Crystal
https://github.com/j8r/crystal-byte-protocol
bytes crystal-lang protocol websocket
Last synced: 3 months ago
JSON representation
A byte protocol for Crystal
- Host: GitHub
- URL: https://github.com/j8r/crystal-byte-protocol
- Owner: j8r
- License: isc
- Created: 2021-01-11T20:05:30.000Z (over 5 years ago)
- Default Branch: master
- Last Pushed: 2023-02-14T18:50:08.000Z (over 3 years ago)
- Last Synced: 2025-04-15T00:41:27.438Z (about 1 year ago)
- Topics: bytes, crystal-lang, protocol, websocket
- Language: Crystal
- Homepage:
- Size: 32.2 KB
- Stars: 4
- Watchers: 2
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# Crystal Byte Protocol
[](https://github.com/j8r/crystal-byte-protocol/actions?query=workflow%3ACI)
[](https://j8r.github.io/crystal-byte-protocol)
[](https://en.wikipedia.org/wiki/ISC_license)
## Installation
Add the dependency to your `shard.yml`:
```yaml
dependencies:
crystal-byte-protocol:
github: j8r/crystal-byte-protocol
```
## Documentation
https://j8r.github.io/crystal-byte-protocol
## Usage
See the [specs](./specs) to have concrete examples.
### Generate JS protocol messages
In a `bin/gen-js-protocol-messages.cr` file:
```cr
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"`
```js
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