rocket_chat-realtime 0.1.0 → 0.1.1

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 66fc24b35954885a75af20a2cd44fbb3159ae405e6e2bd41d0a47caf751055e3
4
- data.tar.gz: 51380a853e80b9409fe9feb2aa490c476da53aa2f78205d16c550c73b7f7907e
3
+ metadata.gz: 4bb47488855acda343dbd9c40235cd4c1335a84054fe398d316a667c82882bdd
4
+ data.tar.gz: 8afcf13cd2d6ae9a7e0b6fb0ed4cb773b3534bd6e74fac8e873c23d72343702c
5
5
  SHA512:
6
- metadata.gz: e8532f7fdd44a92966eedbe0f651af1f0c20dfe4fd07604b8286575bb90530227c02e4390e9e7786afb3b8228080189e0f35fe7bdbcd2db76e214d4efb827161
7
- data.tar.gz: '0359647a097c3b7ac777f41121defb22ee4439a5abcac3bfec3f538a8b3480f0b0aa593bbc27fe46d0a050aaa151b6b8aba53847952850ec1d9fb8e54df0c1f0'
6
+ metadata.gz: 5a5cc9722b8aa5edce1de87ebebe1f8cf4cbfb14496460f37505aead18d9bbbdba03492d694dd8dba76021a59ff32aacfefdf193845ea7fe4e4ac19b49619cd9
7
+ data.tar.gz: a2b35855823bc1f0ab8c6e06023d8bab32bb18161ebf2464134981c793f1550d0ae20f6268e654477647bf602806eb8d1b3d968d0c2ccc7ff8e91cd8c332f640
@@ -1,7 +1,7 @@
1
1
  PATH
2
2
  remote: .
3
3
  specs:
4
- rocket_chat-realtime (0.1.0)
4
+ rocket_chat-realtime (0.1.1)
5
5
  concurrent-ruby (~> 1.1)
6
6
  nio4r (~> 2.5)
7
7
  websocket-driver (~> 0.7)
@@ -22,7 +22,7 @@ GEM
22
22
  docile (1.3.2)
23
23
  iniparse (1.5.0)
24
24
  method_source (1.0.0)
25
- nio4r (2.5.3)
25
+ nio4r (2.5.4)
26
26
  overcommit (0.55.0)
27
27
  childprocess (>= 0.6.3, < 5)
28
28
  iniparse (~> 1.4)
@@ -34,7 +34,7 @@ GEM
34
34
  method_source (~> 1.0)
35
35
  rainbow (3.0.0)
36
36
  rake (12.3.3)
37
- regexp_parser (1.7.1)
37
+ regexp_parser (1.8.0)
38
38
  rexml (3.2.4)
39
39
  rspec (3.9.0)
40
40
  rspec-core (~> 3.9.0)
@@ -60,19 +60,20 @@ GEM
60
60
  rubocop-ast (>= 0.3.0, < 1.0)
61
61
  ruby-progressbar (~> 1.7)
62
62
  unicode-display_width (>= 1.4.0, < 2.0)
63
- rubocop-ast (0.3.0)
63
+ rubocop-ast (0.4.2)
64
64
  parser (>= 2.7.1.4)
65
- rubocop-performance (1.7.1)
66
- rubocop (>= 0.82.0)
67
- rubocop-rspec (1.41.0)
68
- rubocop (>= 0.68.1)
65
+ rubocop-performance (1.8.1)
66
+ rubocop (>= 0.87.0)
67
+ rubocop-ast (>= 0.4.0)
68
+ rubocop-rspec (1.43.2)
69
+ rubocop (~> 0.87)
69
70
  rubocop-thread_safety (0.4.1)
70
71
  rubocop (>= 0.51.0)
71
72
  ruby-progressbar (1.10.1)
72
73
  simplecov (0.19.0)
73
74
  docile (~> 1.1)
74
75
  simplecov-html (~> 0.11)
75
- simplecov-html (0.12.2)
76
+ simplecov-html (0.12.3)
76
77
  thor (1.0.1)
77
78
  unicode-display_width (1.7.0)
78
79
  websocket-driver (0.7.3)
data/README.md CHANGED
@@ -1,8 +1,6 @@
1
1
  # RocketChat::Realtime
2
2
 
3
- Welcome to your new gem! In this directory, you'll find the files you need to be able to package up your Ruby library into a gem. Put your Ruby code in the file `lib/rocket_chat/realtime`. To experiment with that code, run `bin/console` for an interactive prompt.
4
-
5
- TODO: Delete this and the text above, and describe your gem
3
+ The ruby implement for [RocketChat Realtime API](https://docs.rocket.chat/api/realtime-api).
6
4
 
7
5
  ## Installation
8
6
 
@@ -22,7 +20,61 @@ Or install it yourself as:
22
20
 
23
21
  ## Usage
24
22
 
25
- TODO: Write usage instructions here
23
+ ### Create a client
24
+
25
+ ```ruby
26
+ client = RocketChat::Realtime::Client.new(server: 'wss://chat.example.com')
27
+ ```
28
+
29
+ ### Connect to server
30
+
31
+ ```ruby
32
+ client.connect
33
+ ```
34
+
35
+ or use a shortcut via `.connect`
36
+
37
+ ```ruby
38
+ client = RocketChat::Realtime.connect(server: 'wss://chat.example.com')
39
+ ```
40
+
41
+ ### Login to server
42
+
43
+ ```ruby
44
+ client.login(username, password)
45
+ ```
46
+
47
+ The Realtime API is async and depend on the Metro.js DDP, we can use `Concurrent::Promises` to capture the return message.
48
+
49
+ ```ruby
50
+ client
51
+ .login(username, password)
52
+ .then { |res| puts "Token is #{res['token']}" }
53
+ ```
54
+
55
+ ### Subscribe room messages
56
+
57
+ ```ruby
58
+ client
59
+ .subscribe_room_messages('ROOM_ID')
60
+ .then { puts 'Ready!' }
61
+ ```
62
+
63
+ ### Process room messages
64
+
65
+ ```ruby
66
+ client.on('stream-room-messages') do |message|
67
+ puts "Received message: #{message['args'][0]['msg']}"
68
+ end
69
+ ```
70
+
71
+ ### Send message
72
+
73
+ ```ruby
74
+ client
75
+ .send_message('ROOM_ID', 'MESSAGE')
76
+ .then { |res| puts 'Success' }
77
+ ```
26
78
 
27
79
  ## Development
28
80
 
@@ -32,9 +84,9 @@ To install this gem onto your local machine, run `bundle exec rake install`. To
32
84
 
33
85
  ## Contributing
34
86
 
35
- Bug reports and pull requests are welcome on GitHub at https://github.com/[USERNAME]/rocket_chat-realtime. This project is intended to be a safe, welcoming space for collaboration, and contributors are expected to adhere to the [code of conduct](https://github.com/[USERNAME]/rocket_chat-realtime/blob/master/CODE_OF_CONDUCT.md).
87
+ Bug reports and pull requests are welcome on GitHub at https://github.com/5xRuby/rocket_chat-realtime. This project is intended to be a safe, welcoming space for collaboration, and contributors are expected to adhere to the [code of conduct](https://github.com/5xRuby/rocket_chat-realtime/blob/master/CODE_OF_CONDUCT.md).
36
88
 
37
89
 
38
90
  ## Code of Conduct
39
91
 
40
- Everyone interacting in the RocketChat::Realtime project's codebases, issue trackers, chat rooms and mailing lists is expected to follow the [code of conduct](https://github.com/[USERNAME]/rocket_chat-realtime/blob/master/CODE_OF_CONDUCT.md).
92
+ Everyone interacting in the RocketChat::Realtime project's codebases, issue trackers, chat rooms and mailing lists is expected to follow the [code of conduct](https://github.com/5xRuby/rocket_chat-realtime/blob/master/CODE_OF_CONDUCT.md).
@@ -7,17 +7,26 @@ module RocketChat
7
7
  # @since 0.1.0
8
8
  class Adapter
9
9
  # @since 0.1.0
10
- attr_reader :url
10
+ attr_reader :url, :monitor
11
11
 
12
12
  # @param url [String] the server to connect
13
13
  #
14
14
  # @since 0.1.0
15
- def initialize(url)
15
+ def initialize(url, monitor)
16
16
  @url = url
17
+ @monitor = monitor
17
18
  @mutex = Mutex.new
18
19
  @buffer = ''
19
20
  end
20
21
 
22
+ def pending?
23
+ @buffer.empty? == false
24
+ end
25
+
26
+ def dispose
27
+ @monitor = nil
28
+ end
29
+
21
30
  # Enqueue data to send to server
22
31
  #
23
32
  # @param data [String]
@@ -25,6 +34,8 @@ module RocketChat
25
34
  # @since 0.1.0
26
35
  def write(data)
27
36
  @mutex.synchronize { @buffer = @buffer.dup.concat(data) }
37
+ pump_buffer
38
+ Reactor.selector.wakeup unless monitor.interests == :r
28
39
  end
29
40
 
30
41
  # Pump Buffer
@@ -34,17 +45,17 @@ module RocketChat
34
45
  # @return [Number] total bytes written
35
46
  #
36
47
  # @since 0.1.0
37
- def pump_buffer(io)
48
+ def pump_buffer
38
49
  @mutex.synchronize do
39
50
  written = 0
40
- begin
41
- written = io.write_nonblock @buffer unless @buffer.empty?
42
- @buffer = @buffer.byteslice(written..-1) if written.positive?
43
- rescue IO::WaitWritable, IO::WaitReadable
44
- return written
45
- end
51
+ written = monitor.io.write_nonblock @buffer unless @buffer.empty?
52
+ @buffer = @buffer.byteslice(written..-1) if written.positive?
46
53
  written
47
54
  end
55
+ rescue IO::WaitWritable, IO::WaitReadable
56
+ written
57
+ ensure
58
+ monitor.interests = pending? ? :rw : :r
48
59
  end
49
60
  end
50
61
  end
@@ -33,7 +33,7 @@ module RocketChat
33
33
  delegate %w[ping pong] => :driver
34
34
 
35
35
  # @since 0.1.0
36
- attr_reader :server, :connector, :adapter, :driver
36
+ attr_reader :server, :connector, :adapter, :driver, :dispatcher
37
37
 
38
38
  # @param options [Hash]
39
39
  #
@@ -41,9 +41,6 @@ module RocketChat
41
41
  def initialize(options = {})
42
42
  @server = options[:server]
43
43
  @connector = Connector.new(endpoint)
44
- @adapter = Adapter.new(endpoint)
45
- @driver = WebSocket::Driver.client(adapter)
46
- @dispatcher = Dispatcher.new(self)
47
44
  end
48
45
 
49
46
  # @return [String] the realtime api endpoint
@@ -53,19 +50,32 @@ module RocketChat
53
50
  "#{server}/websocket"
54
51
  end
55
52
 
53
+ # Add to reactor
54
+ #
55
+ # @since 0.1.0
56
+ def add_to_reactor
57
+ monitor = Reactor.register(self)
58
+ return if monitor.nil?
59
+
60
+ @adapter = Adapter.new(endpoint, monitor)
61
+ @driver = WebSocket::Driver.client(adapter)
62
+ @dispatcher = Dispatcher.new(self)
63
+ end
64
+
56
65
  # Connect to server
57
66
  #
58
67
  # @since 0.1.0
59
68
  def connect
69
+ add_to_reactor
60
70
  driver.start
61
71
  driver.text(INITIALIZE_COMMAND.to_json)
62
- Reactor.register(self)
63
72
  end
64
73
 
65
74
  # Close connection to server
66
75
  #
67
76
  # @since 0.1.0
68
77
  def disconnect
78
+ @adapter.dispose
69
79
  @dispatcher.dispose
70
80
  driver.close
71
81
  Reactor.deregister(self)
@@ -86,8 +96,9 @@ module RocketChat
86
96
  #
87
97
  # @since 0.1.0
88
98
  def process(monitor)
99
+ monitor.interests = adapter.pending? ? :rw : :r
89
100
  driver.parse(monitor.io.read_nonblock(2**14)) if monitor.readable?
90
- adapter.pump_buffer(monitor.io) if monitor.writeable?
101
+ adapter.pump_buffer if monitor.writeable?
91
102
  rescue IO::WaitReadable, IO::WaitWritable
92
103
  # nope
93
104
  rescue Errno::ECONNRESET, EOFError, Errno::ECONNABORTED
@@ -25,6 +25,7 @@ module RocketChat
25
25
  stop
26
26
  stopped?
27
27
  reset
28
+ wakeup
28
29
  ] => :instance
29
30
  end
30
31
 
@@ -64,6 +65,7 @@ module RocketChat
64
65
  @clients.add(client)
65
66
  monitor = selector.register(client.connector.socket, :rw)
66
67
  monitor.value = client
68
+ monitor
67
69
  end
68
70
 
69
71
  # Deregister Client
@@ -2,6 +2,6 @@
2
2
 
3
3
  module RocketChat
4
4
  module Realtime
5
- VERSION = '0.1.0'
5
+ VERSION = '0.1.1'
6
6
  end
7
7
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: rocket_chat-realtime
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.0
4
+ version: 0.1.1
5
5
  platform: ruby
6
6
  authors:
7
7
  - 5xRuby
8
8
  autorequire:
9
9
  bindir: exe
10
10
  cert_chain: []
11
- date: 2020-09-18 00:00:00.000000000 Z
11
+ date: 2020-09-24 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: concurrent-ruby