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 +4 -4
- data/lib/rakie/channel.rb +9 -4
- data/lib/rakie/http_server.rb +10 -3
- data/lib/rakie/version.rb +1 -1
- data/lib/rakie/websocket.rb +58 -37
- data/lib/rakie/websocket_server.rb +10 -4
- metadata +1 -1
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: d898a3567f82f46d55e31f84ee421ef63ad4818dea4897f44b226700136a8dc9
|
4
|
+
data.tar.gz: 2adb4b03156ad6ea22d18a5c6e0b42a0044bf4d76949f6156f98f5ea129cfe4a
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
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")
|
data/lib/rakie/http_server.rb
CHANGED
@@ -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
|
19
|
-
@
|
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
data/lib/rakie/websocket.rb
CHANGED
@@ -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
|
-
|
26
|
+
Log.debug("Rakie::Websocket recv: #{data}")
|
26
27
|
|
27
|
-
|
28
|
-
message = @recv_message
|
28
|
+
total_parsed = 0
|
29
29
|
|
30
|
-
|
31
|
-
|
32
|
-
|
33
|
-
end
|
30
|
+
while data.length > 0
|
31
|
+
# @type [WebsocketMessage] request
|
32
|
+
message = @recv_message
|
34
33
|
|
35
|
-
|
34
|
+
if message.parse_status == ParseStatus::COMPLETE
|
35
|
+
message = WebsocketMessage.new
|
36
|
+
@recv_message = message
|
37
|
+
end
|
36
38
|
|
37
|
-
|
39
|
+
len = message.parse(data)
|
40
|
+
total_parsed += len
|
38
41
|
|
39
|
-
|
40
|
-
response = WebsocketMessage.new
|
42
|
+
Log.debug("Rakie::Websocket receive message: #{message.to_s} parse with #{len}")
|
41
43
|
|
42
|
-
if message.
|
43
|
-
response
|
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
|
-
|
48
|
-
|
49
|
-
|
50
|
-
|
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
|
-
|
53
|
-
|
54
|
-
|
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
|
-
|
57
|
-
|
58
|
-
|
59
|
-
|
60
|
-
|
61
|
-
|
62
|
-
|
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
|
-
|
74
|
+
Log.debug("Rakie::Websocket response: #{response_data}")
|
65
75
|
|
66
|
-
|
76
|
+
channel.write(response_data) # Response data
|
67
77
|
|
68
|
-
|
69
|
-
|
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
|
-
|
72
|
-
return len
|
92
|
+
data = data[len .. -1]
|
73
93
|
end
|
74
94
|
|
75
|
-
return
|
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
|
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(
|
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
|