Class: Rage::Cable::Protocols::RawWebSocketJson
- Inherits:
-
Base
- Object
- Base
- Rage::Cable::Protocols::RawWebSocketJson
show all
- Defined in:
- lib/rage/cable/protocols/raw_web_socket_json.rb
Overview
The RawWebSocketJson protocol allows a direct connection to a Rage::Cable application using the native
WebSocket object. With this protocol, each WebSocket connection directly corresponds to a single
channel subscription. As a result, clients are automatically subscribed to a channel as soon as
they establish a connection.
Heartbeats are also supported - the server will respond with pong to every ping message. Additionally,
all ping messages are buffered and processed once a second, which means it can take up to a second for
the server to respond to a ping.
Class Method Summary
collapse
Methods inherited from Base
broadcast, on_remote_disconnect, protocol_definition
Class Method Details
.init ⇒ Object
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
|
# File 'lib/rage/cable/protocols/raw_web_socket_json.rb', line 47
def self.init
super
@ping_connections = Set.new
Iodine.on_state(:on_start) do
Iodine.run_every(1_000) do
@ping_connections.each_slice(500) do |slice|
Iodine.defer { slice.each { |connection| connection.write("pong") } }
end
@ping_connections.clear
end
end
end
|
.on_close(connection, env, router) ⇒ Object
126
127
128
|
# File 'lib/rage/cable/protocols/raw_web_socket_json.rb', line 126
def self.on_close(connection, env, router)
router.process_disconnection(connection, env)
end
|
.on_message(connection, env, raw_data, router) ⇒ Object
109
110
111
112
113
114
115
116
117
118
119
120
121
|
# File 'lib/rage/cable/protocols/raw_web_socket_json.rb', line 109
def self.on_message(connection, env, raw_data, router)
if raw_data == "ping"
@ping_connections << connection
return
end
data = JSON.parse(raw_data)
message_status = router.process_message(connection, env, IDENTIFIER, :receive, data)
unless message_status == :processed
connection.write(MESSAGES::UNKNOWN)
end
end
|
.on_open(connection, env, router) ⇒ Object
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
|
# File 'lib/rage/cable/protocols/raw_web_socket_json.rb', line 66
def self.on_open(connection, env, router)
accepted = router.process_connection(connection, env)
unless accepted
connection.write(MESSAGES::UNAUTHORIZED)
connection.close
return
end
channel_id = env["PATH_INFO"].split("/")[-1]
channel_name = if channel_id.end_with?("Channel")
channel_id
else
if channel_id.include?("_")
tmp = ""
channel_id.split("_") { |segment| tmp += segment.capitalize! || segment }
channel_id = tmp
else
channel_id.capitalize!
end
"#{channel_id}Channel"
end
query_string = env["QUERY_STRING"]
params = query_string == "" ? DEFAULT_PARAMS : Iodine::Rack::Utils.parse_nested_query(query_string)
status = router.process_subscription(connection, env, IDENTIFIER, channel_name, params)
if status == :rejected
connection.write(MESSAGES::REJECTED)
connection.close
elsif status == :invalid
connection.write(MESSAGES::INVALID)
connection.close
end
end
|
.protocol_id ⇒ String
A unique identifier of the protocol used in multi-server setups by PubSub.
142
143
144
|
# File 'lib/rage/cable/protocols/raw_web_socket_json.rb', line 142
def self.protocol_id
"rawjson"
end
|
.serialize(_, data) ⇒ Object
131
132
133
|
# File 'lib/rage/cable/protocols/raw_web_socket_json.rb', line 131
def self.serialize(_, data)
data.to_json
end
|
.supports_rpc? ⇒ Boolean
136
137
138
|
# File 'lib/rage/cable/protocols/raw_web_socket_json.rb', line 136
def self.supports_rpc?
false
end
|