rakie 0.0.6 → 0.0.11

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: 6aa5c872ec3bdf47d4f33f08f12f6ba6c3521553c9411c284d01b5efc6a63431
4
- data.tar.gz: 0ed904078c4aa1bdbf6d9564be3b381a5ae6ca1c2831256c2fadff5cd449c81d
3
+ metadata.gz: d898a3567f82f46d55e31f84ee421ef63ad4818dea4897f44b226700136a8dc9
4
+ data.tar.gz: 2adb4b03156ad6ea22d18a5c6e0b42a0044bf4d76949f6156f98f5ea129cfe4a
5
5
  SHA512:
6
- metadata.gz: 80823efc97c0d28c64d0871149118a5df0bebd19623a7b7088b9831218e8e0cee8e8a32901a010af3ffdb5e24ce86d5892dac8f5638541427e151e60209738c2
7
- data.tar.gz: 9c747c282c958a45b307eba5e9bf451f513c8337106a4c1aaf59b8b6b4809ce915607e91ca544af48149b9faa7418041fea03adf6fca06580014bd1c51765308
6
+ metadata.gz: ce2fdecade1fcc90be23f2a3df87512a0889662307c07ae32a3495b861de5fbe20c91f971f3924c6a5f05f307b978c71cb9f4781285d32b0d0f189565245df58
7
+ data.tar.gz: f7c254d194e79de3c9cd7befbacc7cf8409e258b68fce01b316af0ba46458450b0b6dde47b41f83ab93cc6783e17d0b84f400ad84bd22f0c591ce35f5f4a02f6
data/lib/rakie/channel.rb CHANGED
@@ -104,15 +104,20 @@ module Rakie
104
104
  end
105
105
 
106
106
  def on_detach(io)
107
+ if io.closed?
108
+ return
109
+ end
110
+
107
111
  begin
108
112
  io.close
109
113
 
110
- if @delegate
111
- @delegate.on_close(self)
112
- end
113
-
114
114
  rescue
115
115
  Log.debug("Channel is already closed")
116
+ return
117
+ end
118
+
119
+ if @delegate
120
+ @delegate.on_close(self)
116
121
  end
117
122
 
118
123
  Log.debug("Channel close ok")
@@ -13,10 +13,13 @@ module Rakie
13
13
  end
14
14
  end
15
15
 
16
- attr_reader :channel, :opt
16
+ attr_reader :channel, :opt, :host, :port
17
17
 
18
- def initialize(delegate=nil)
19
- @channel = TCPServerChannel.new('127.0.0.1', 10086, self)
18
+ def initialize(host: '127.0.0.1', port: 10086, delegate: nil)
19
+ @host = host
20
+ @port = port
21
+
22
+ @channel = TCPServerChannel.new(host, port, self)
20
23
 
21
24
  # @type [Hash{Channel=>Session}]
22
25
  @sessions = {}
@@ -123,5 +126,9 @@ module Rakie
123
126
  end
124
127
  end
125
128
  end
129
+
130
+ def on_close(channel)
131
+ @sessions.delete(channel)
132
+ end
126
133
  end
127
134
  end
data/lib/rakie/version.rb CHANGED
@@ -1,5 +1,5 @@
1
1
  module Rakie
2
- VERSION = [0, 0, 6]
2
+ VERSION = [0, 0, 11]
3
3
 
4
4
  def self.version_s
5
5
  VERSION.join('.')
@@ -21,58 +21,78 @@ module Rakie
21
21
  @client_side = true
22
22
  end
23
23
 
24
+ # @param [String] data
24
25
  def on_recv(channel, data)
25
- # Log.debug("Rakie::HTTPServer recv: #{data}")
26
+ Log.debug("Rakie::Websocket recv: #{data}")
26
27
 
27
- # @type [WebsocketMessage] request
28
- message = @recv_message
28
+ total_parsed = 0
29
29
 
30
- if message.parse_status == ParseStatus::COMPLETE
31
- message = WebsocketMessage.new
32
- @recv_message = message
33
- end
30
+ while data.length > 0
31
+ # @type [WebsocketMessage] request
32
+ message = @recv_message
34
33
 
35
- len = message.parse(data)
34
+ if message.parse_status == ParseStatus::COMPLETE
35
+ message = WebsocketMessage.new
36
+ @recv_message = message
37
+ end
36
38
 
37
- Log.debug("Rakie::Websocket receive message: #{message.to_s}")
39
+ len = message.parse(data)
40
+ total_parsed += len
38
41
 
39
- if message.parse_status == ParseStatus::COMPLETE
40
- response = WebsocketMessage.new
42
+ Log.debug("Rakie::Websocket receive message: #{message.to_s} parse with #{len}")
41
43
 
42
- if message.op_code == WebsocketMessage::OP_PING
43
- response.fin = true
44
- response.op_code = WebsocketMessage::OP_PONG
45
- response.payload = "pong"
44
+ if message.parse_status == ParseStatus::COMPLETE
45
+ response = WebsocketMessage.new
46
46
 
47
- elsif message.op_code == WebsocketMessage::OP_PONG
48
- response.fin = true
49
- response.op_code = WebsocketMessage::OP_PING
50
- response.payload = "ping"
47
+ if message.op_code == WebsocketMessage::OP_PING
48
+ response.fin = true
49
+ response.op_code = WebsocketMessage::OP_PONG
50
+ response.payload = "Rakie::Websocket: op pong"
51
51
 
52
- elsif @delegate
53
- @delegate.on_message(self, message.payload)
54
- return len
52
+ elsif message.op_code == WebsocketMessage::OP_PONG
53
+ response.fin = true
54
+ response.op_code = WebsocketMessage::OP_PING
55
+ response.payload = "Rakie::Websocket: op ping"
55
56
 
56
- else
57
- response.fin = true
58
- response.op_code = WebsocketMessage::OP_TEXT
59
- response.payload = "Rakie!"
60
- end
61
-
62
- response_data = response.to_s
57
+ elsif message.op_code == WebsocketMessage::OP_CLOSE
58
+ channel.close
59
+
60
+ Log.debug("Rakie::Websocket: op close")
61
+ return 0
62
+
63
+ elsif @delegate
64
+ @delegate.on_message(self, message.payload)
65
+
66
+ else
67
+ response.fin = true
68
+ response.op_code = WebsocketMessage::OP_TEXT
69
+ response.payload = "Rakie!"
70
+ end
71
+
72
+ response_data = response.to_s
63
73
 
64
- Log.debug("Rakie::Websocket response: #{response_data}")
74
+ Log.debug("Rakie::Websocket response: #{response_data}")
65
75
 
66
- channel.write(response_data) # Response data
76
+ channel.write(response_data) # Response data
67
77
 
68
- elsif message.parse_status == ParseStatus::ERROR
69
- channel.close
78
+ elsif message.parse_status == ParseStatus::CONTINUE
79
+ break
80
+
81
+ elsif message.parse_status == ParseStatus::ERROR
82
+ channel.close
83
+
84
+ Log.debug("Rakie::Websocket: Illegal message")
85
+ return 0
86
+ end
87
+
88
+ if len >= data.length
89
+ break
90
+ end
70
91
 
71
- Log.debug("Rakie::Websocket: Illegal message")
72
- return len
92
+ data = data[len .. -1]
73
93
  end
74
94
 
75
- return len
95
+ return total_parsed
76
96
  end
77
97
 
78
98
  def on_send(channel)
@@ -109,6 +129,7 @@ module Rakie
109
129
  end
110
130
 
111
131
  send_message = ws_message.to_s
132
+ @send_messages << ws_message
112
133
 
113
134
  Log.debug("Rakie::Websocket send: #{send_message}")
114
135
 
@@ -134,4 +155,4 @@ module Rakie
134
155
  @channel.write(send_message) # Response data
135
156
  end
136
157
  end
137
- end
158
+ end
@@ -1,13 +1,16 @@
1
1
  module Rakie
2
2
  class WebsocketServer < Websocket
3
3
  # @param [Rakie::HttpServer] http_server
4
- def initialize(delegate=nil, http_server=nil)
4
+ def initialize(host: '127.0.0.1', port: 10086, delegate: nil, http_server: nil)
5
5
  @delegate = delegate
6
6
 
7
7
  if http_server == nil
8
- http_server = HttpServer.new('127.0.0.1', 10086, self)
8
+ http_server = HttpServer.new(host: host, port: port)
9
9
  end
10
10
 
11
+ @host = http_server.host
12
+ @port = http_server.port
13
+
11
14
  http_server.opt[:websocket_delegate] = self
12
15
  @channel = http_server.channel
13
16
 
@@ -23,7 +26,6 @@ module Rakie
23
26
 
24
27
  if @delegate
25
28
  @delegate.on_connect(ws_client)
26
- return
27
29
  end
28
30
 
29
31
  @clients[channel] = ws_client
@@ -49,8 +51,10 @@ module Rakie
49
51
  client = @clients[channel]
50
52
 
51
53
  if client
52
- client.on_recv(channel, data)
54
+ return client.on_recv(channel, data)
53
55
  end
56
+
57
+ return data.length
54
58
  end
55
59
 
56
60
  def on_send(channel)
@@ -67,6 +71,8 @@ module Rakie
67
71
  if client
68
72
  client.on_close(channel)
69
73
  end
74
+
75
+ @clients.delete(channel)
70
76
  end
71
77
 
72
78
  def send(message, is_binary=false); end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: rakie
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.6
4
+ version: 0.0.11
5
5
  platform: ruby
6
6
  authors:
7
7
  - Jakit Liang