json-socket 1.0.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (3) hide show
  1. checksums.yaml +7 -0
  2. data/lib/json-socket.rb +88 -0
  3. metadata +73 -0
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA256:
3
+ metadata.gz: e68046e17ebd89adb2e51d3a84004153267794bb3f7eab627a2adf86656128c4
4
+ data.tar.gz: 2f1336e84ef7f97ca5d4f6a30a4ddb375a311093c90eab2d0005a4dabb62f0c6
5
+ SHA512:
6
+ metadata.gz: 91b92b004df4b35f1f9ac0cc281ae1e255f8e4db66f1bcfbfd766052056e17c90f7e74e13755facbe17b8748dce69ab864b93ce613284d6dc67e550043cea802
7
+ data.tar.gz: 2362b87bdcde12c4c74b79507d69d8b4de4fbd21cec77e464046b5677bc142607df40fc55880b7a9f1183df9a3c0901f250cf2f0e36f9412dc2bc74e9ec222e8
@@ -0,0 +1,88 @@
1
+ require "socket"
2
+ require "yajl"
3
+ require 'fileutils'
4
+
5
+ module JSONSocket
6
+ class Server
7
+
8
+ def initialize(host: "127.0.0.1", port: 1234, delimeter: "#", unix_socket: nil)
9
+ @delimeter = delimeter
10
+ @stop = false
11
+ @server = if unix_socket
12
+ FileUtils.rm_f(unix_socket)
13
+ UNIXServer.new unix_socket
14
+ else
15
+ TCPServer.new host, port
16
+ end
17
+ end
18
+
19
+ def stop
20
+ @stop = true
21
+ end
22
+
23
+ def listen
24
+ loop do
25
+ Thread.start(@server.accept) do |client|
26
+ begin
27
+ message_length = client.gets(@delimeter).to_i
28
+ on_message(Yajl::Parser.parse(client.read(message_length)), client)
29
+ rescue Exception => e
30
+ STDERR.puts e.message
31
+ end
32
+ end
33
+ break if @stop
34
+ end
35
+ end
36
+
37
+ def on_message(message, client)
38
+ puts "Default on_message method - please override like this: \n" \
39
+ " \n" \
40
+ "class CustomJSONSocketServer < JSONSocket::Server \n" \
41
+ " \n" \
42
+ " def on_message(message, client)\n" \
43
+ " puts message \n" \
44
+ " self.send_end_message([1,2,3], client)\n" \
45
+ " end \n" \
46
+ " \n" \
47
+ "end \n"
48
+ client.close
49
+ end
50
+
51
+ def send_end_message(message, client)
52
+ strigified = Yajl::Encoder.encode(message)
53
+ client << "#{strigified.bytesize}#{@delimeter}#{strigified}"
54
+ client.close
55
+ end
56
+
57
+ end
58
+
59
+ class Client
60
+ def initialize(host: "127.0.0.1", port: 1234, delimeter: "#", unix_socket: nil)
61
+ @delimeter = delimeter
62
+ @unix_socket = unix_socket
63
+ @host = host
64
+ @port = port
65
+ end
66
+
67
+ def handle_send_receive(socket, message)
68
+ strigified = Yajl::Encoder.encode(message)
69
+ socket << "#{strigified.bytesize}#{@delimeter}#{strigified}"
70
+ message_length = socket.gets(@delimeter).to_i
71
+ return Yajl::Parser.parse(socket.read(message_length))
72
+ end
73
+
74
+ def send(message)
75
+ begin
76
+ if @unix_socket
77
+ UNIXSocket.open(@unix_socket) {| socket| handle_send_receive(socket, message) }
78
+ else
79
+ TCPSocket.open(@host, @port) {| socket| handle_send_receive(socket, message) }
80
+ end
81
+ rescue Exception => e
82
+ STDERR.puts e
83
+ end
84
+ end
85
+
86
+ end
87
+
88
+ end
metadata ADDED
@@ -0,0 +1,73 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: json-socket
3
+ version: !ruby/object:Gem::Version
4
+ version: 1.0.0
5
+ platform: ruby
6
+ authors:
7
+ - foi
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2018-07-09 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: yajl-ruby
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - ">="
18
+ - !ruby/object:Gem::Version
19
+ version: '0'
20
+ type: :runtime
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - ">="
25
+ - !ruby/object:Gem::Version
26
+ version: '0'
27
+ - !ruby/object:Gem::Dependency
28
+ name: rspec
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - ">="
32
+ - !ruby/object:Gem::Version
33
+ version: '0'
34
+ type: :development
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - ">="
39
+ - !ruby/object:Gem::Version
40
+ version: '0'
41
+ description: JSON-socket client & server implementation. Inspired by and compatible
42
+ with sebastianseilund/node-json-socket
43
+ email: foi@live.ru
44
+ executables: []
45
+ extensions: []
46
+ extra_rdoc_files: []
47
+ files:
48
+ - lib/json-socket.rb
49
+ homepage: https://github.com/foi/ruby-json-socket
50
+ licenses:
51
+ - MIT
52
+ metadata: {}
53
+ post_install_message:
54
+ rdoc_options: []
55
+ require_paths:
56
+ - lib
57
+ required_ruby_version: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - ">="
60
+ - !ruby/object:Gem::Version
61
+ version: '0'
62
+ required_rubygems_version: !ruby/object:Gem::Requirement
63
+ requirements:
64
+ - - ">="
65
+ - !ruby/object:Gem::Version
66
+ version: '0'
67
+ requirements: []
68
+ rubyforge_project:
69
+ rubygems_version: 2.7.6
70
+ signing_key:
71
+ specification_version: 4
72
+ summary: json-socket protocol implementation
73
+ test_files: []