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.
Files changed (3) hide show
  1. checksums.yaml +4 -4
  2. data/lib/json-socket.rb +29 -6
  3. metadata +3 -3
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: e68046e17ebd89adb2e51d3a84004153267794bb3f7eab627a2adf86656128c4
4
- data.tar.gz: 2f1336e84ef7f97ca5d4f6a30a4ddb375a311093c90eab2d0005a4dabb62f0c6
3
+ metadata.gz: 33170988476a7155bf6a8375019eab67d034a18faa6a4b31b12b4f59c43aca66
4
+ data.tar.gz: 0dbf13616b22f1810140e8243aa230466309f4b8063bc4a095b0386eaaeef04f
5
5
  SHA512:
6
- metadata.gz: 91b92b004df4b35f1f9ac0cc281ae1e255f8e4db66f1bcfbfd766052056e17c90f7e74e13755facbe17b8748dce69ab864b93ce613284d6dc67e550043cea802
7
- data.tar.gz: 2362b87bdcde12c4c74b79507d69d8b4de4fbd21cec77e464046b5677bc142607df40fc55880b7a9f1183df9a3c0901f250cf2f0e36f9412dc2bc74e9ec222e8
6
+ metadata.gz: 6eeab193107952cf98e36e6a506a71e37b952be649d71cb65f1edc4cb887e9711cbdd9cfa85d7bb0f30e6af2e6f5ea11b59ef010a527ca35236c561d2754ee46
7
+ data.tar.gz: 4f99730df94bf9a65d9a56d41501927fa7056ff3d4af47f8c2d0605428e5a0aead95d259ac2024a2193460e35f17f04b0249d33a6c3045db64c541eee9d5dc5a
@@ -1,10 +1,26 @@
1
1
  require "socket"
2
- require "yajl"
3
- require 'fileutils'
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(Yajl::Parser.parse(client.read(message_length)), client)
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
- strigified = Yajl::Encoder.encode(message)
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
- strigified = Yajl::Encoder.encode(message)
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 Yajl::Parser.parse(socket.read(message_length))
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.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: yajl-ruby
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.6
69
+ rubygems_version: 2.7.7
70
70
  signing_key:
71
71
  specification_version: 4
72
72
  summary: json-socket protocol implementation