json-socket 1.0.0 → 1.0.1
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/lib/json-socket.rb +29 -6
- metadata +3 -3
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 33170988476a7155bf6a8375019eab67d034a18faa6a4b31b12b4f59c43aca66
|
4
|
+
data.tar.gz: 0dbf13616b22f1810140e8243aa230466309f4b8063bc4a095b0386eaaeef04f
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 6eeab193107952cf98e36e6a506a71e37b952be649d71cb65f1edc4cb887e9711cbdd9cfa85d7bb0f30e6af2e6f5ea11b59ef010a527ca35236c561d2754ee46
|
7
|
+
data.tar.gz: 4f99730df94bf9a65d9a56d41501927fa7056ff3d4af47f8c2d0605428e5a0aead95d259ac2024a2193460e35f17f04b0249d33a6c3045db64c541eee9d5dc5a
|
data/lib/json-socket.rb
CHANGED
@@ -1,10 +1,26 @@
|
|
1
1
|
require "socket"
|
2
|
-
require "
|
3
|
-
require
|
2
|
+
require "fileutils"
|
3
|
+
require "oj"
|
4
4
|
|
5
5
|
module JSONSocket
|
6
|
+
|
7
|
+
module JsonEncodeDecode
|
8
|
+
|
9
|
+
def parse_json(string)
|
10
|
+
return Oj.load(string)
|
11
|
+
end
|
12
|
+
|
13
|
+
def encode_json(object)
|
14
|
+
return Oj.dump(object)
|
15
|
+
end
|
16
|
+
|
17
|
+
end
|
18
|
+
|
19
|
+
|
6
20
|
class Server
|
7
21
|
|
22
|
+
include JsonEncodeDecode
|
23
|
+
|
8
24
|
def initialize(host: "127.0.0.1", port: 1234, delimeter: "#", unix_socket: nil)
|
9
25
|
@delimeter = delimeter
|
10
26
|
@stop = false
|
@@ -24,8 +40,10 @@ module JSONSocket
|
|
24
40
|
loop do
|
25
41
|
Thread.start(@server.accept) do |client|
|
26
42
|
begin
|
43
|
+
# https://stackoverflow.com/questions/25303943/how-to-send-a-utf-8-encoded-strings-via-tcpsocket-in-ruby/25305256
|
44
|
+
client.set_encoding 'UTF-8'
|
27
45
|
message_length = client.gets(@delimeter).to_i
|
28
|
-
on_message(
|
46
|
+
on_message(parse_json(client.read(message_length)), client)
|
29
47
|
rescue Exception => e
|
30
48
|
STDERR.puts e.message
|
31
49
|
end
|
@@ -49,7 +67,8 @@ module JSONSocket
|
|
49
67
|
end
|
50
68
|
|
51
69
|
def send_end_message(message, client)
|
52
|
-
|
70
|
+
client.set_encoding 'UTF-8'
|
71
|
+
strigified = encode_json(message)
|
53
72
|
client << "#{strigified.bytesize}#{@delimeter}#{strigified}"
|
54
73
|
client.close
|
55
74
|
end
|
@@ -57,6 +76,9 @@ module JSONSocket
|
|
57
76
|
end
|
58
77
|
|
59
78
|
class Client
|
79
|
+
|
80
|
+
include JsonEncodeDecode
|
81
|
+
|
60
82
|
def initialize(host: "127.0.0.1", port: 1234, delimeter: "#", unix_socket: nil)
|
61
83
|
@delimeter = delimeter
|
62
84
|
@unix_socket = unix_socket
|
@@ -65,10 +87,11 @@ module JSONSocket
|
|
65
87
|
end
|
66
88
|
|
67
89
|
def handle_send_receive(socket, message)
|
68
|
-
|
90
|
+
socket.set_encoding 'UTF-8'
|
91
|
+
strigified = encode_json(message)
|
69
92
|
socket << "#{strigified.bytesize}#{@delimeter}#{strigified}"
|
70
93
|
message_length = socket.gets(@delimeter).to_i
|
71
|
-
return
|
94
|
+
return parse_json(socket.read(message_length))
|
72
95
|
end
|
73
96
|
|
74
97
|
def send(message)
|
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: json-socket
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 1.0.
|
4
|
+
version: 1.0.1
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- foi
|
@@ -11,7 +11,7 @@ cert_chain: []
|
|
11
11
|
date: 2018-07-09 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
|
-
name:
|
14
|
+
name: oj
|
15
15
|
requirement: !ruby/object:Gem::Requirement
|
16
16
|
requirements:
|
17
17
|
- - ">="
|
@@ -66,7 +66,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
|
|
66
66
|
version: '0'
|
67
67
|
requirements: []
|
68
68
|
rubyforge_project:
|
69
|
-
rubygems_version: 2.7.
|
69
|
+
rubygems_version: 2.7.7
|
70
70
|
signing_key:
|
71
71
|
specification_version: 4
|
72
72
|
summary: json-socket protocol implementation
|