Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/foi/crystal-json-socket
JSON-socket client & server implementation. Inspired by and compatible with sebastianseilund/node-json-socket
https://github.com/foi/crystal-json-socket
Last synced: about 1 month ago
JSON representation
JSON-socket client & server implementation. Inspired by and compatible with sebastianseilund/node-json-socket
- Host: GitHub
- URL: https://github.com/foi/crystal-json-socket
- Owner: foi
- License: mit
- Created: 2018-07-05T09:27:28.000Z (over 6 years ago)
- Default Branch: master
- Last Pushed: 2023-02-05T09:39:02.000Z (almost 2 years ago)
- Last Synced: 2024-08-01T17:36:16.680Z (4 months ago)
- Language: Crystal
- Homepage:
- Size: 32.2 KB
- Stars: 13
- Watchers: 1
- Forks: 3
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
- awesome-crystal - crystal-json-socket - JSON-socket client & server implementation. Inspired by and compatible with [node-json-socket](https://github.com/sebastianseilund/node-json-socket/) and [ruby-json-socket](https://github.com/foi/ruby-json-socket) (Network Protocols)
- awesome-crystal - crystal-json-socket - JSON-socket client & server implementation. Inspired by and compatible with [node-json-socket](https://github.com/sebastianseilund/node-json-socket/) and [ruby-json-socket](https://github.com/foi/ruby-json-socket) (Network Protocols)
- awesome-crystal - crystal-json-socket - JSON-socket client & server implementation. Inspired by and compatible with [node-json-socket](https://github.com/sebastianseilund/node-json-socket/) and [ruby-json-socket](https://github.com/foi/ruby-json-socket) (Network Protocols)
README
# crystal json-socket ![master branch](https://github.com/foi/crystal-json-socket/actions/workflows/ci.yml/badge.svg)
JSON-socket client & server implementation. Inspired by and compatible with [sebastianseilund/node-json-socket](https://github.com/sebastianseilund/node-json-socket/) and
[ruby json-socket](https://github.com/foi/ruby-json-socket)## Installation
Add this to your application's `shard.yml`:
```yaml
dependencies:
json-socket:
github: foi/crystal-json-socket
```## Usage
server.cr
```crystal
require "json-socket"struct CustomJSONSocketServer
include JSONSocket::Server
def on_message(message, socket)
puts message
result = (message["a"].as_i + message["b"].as_i) * message["b"].as_i * message["a"].as_i
self.send_end_message({ :result => result}, socket)
end# Additionally you can define a custom on_error handler
def on_error(ex)
puts "#{ex.message}"
endend
server = CustomJSONSocketServer.new("127.0.0.1", 1234) # OR CustomJSONSocketServer.new(unix_socket: "/tmp/json-socket-server.sock", delimeter: "µ")
server.listen
```client.cr
```crystal
require "json-socket"
to_server = JSONSocket::Client.new(host: "127.0.0.1", port: 1234) # OR JSONSocket::Client.new(unix_socket: "/tmp/json-socket-server.sock", delimeter: "µ")
result = to_server.send({ a: 12, b: 8})
if (result)
puts result
end
```client.cr with timeouts in seconds
```crystal
require "json-socket"
to_server = JSONSocket::Client.new(host: "127.0.0.1", port: 1234, read_timeout: 10, write_timeout: 5) # OR JSONSocket::Client.new(unix_socket: "/tmp/json-socket-server.sock", delimeter: "µ", read_timeout: 10, write_timeout: 5)
result = to_server.send({ a: 12, b: 8})
if (result)
puts result
end
```