realtimex 1.0.3 → 1.0.4
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +4 -4
- data/lib/realtimex/connection.rb +49 -3
- data/lib/realtimex/version.rb +1 -1
- data/lib/realtimex.rb +7 -2
- 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: 186c4a4f137225ab7ca45bfa90451ec86b5ec8fad21aeb61b8900bf43b7f5a6e
|
|
4
|
+
data.tar.gz: ace0043ea61e33a3ccffe10a30e879af118179c2757e8a48ee5cc5313bcaa1ac
|
|
5
5
|
SHA512:
|
|
6
|
-
metadata.gz:
|
|
7
|
-
data.tar.gz:
|
|
6
|
+
metadata.gz: 96e9b4348dd23ae60c4e5ff9e0b69826a56f22d6c56a3911ed4379b5b859d9d8c1c7992924abef71f09ed254dd431f8e75ed958bb116a7e178972a52634ceb31
|
|
7
|
+
data.tar.gz: 31de2c7549e275d9586649ac96d8a5468755a1c0508a7822f50338114b6e223a43b064441c86dce4b3f0bf7613a9aaff5f50f886ecc1b8ae63fa67815ce24b3c
|
data/lib/realtimex/connection.rb
CHANGED
|
@@ -11,40 +11,66 @@ end unless URI.respond_to?(:encode)
|
|
|
11
11
|
require 'socket.io-client-simple'
|
|
12
12
|
|
|
13
13
|
class RealtimeX::Connection
|
|
14
|
-
def initialize(url)
|
|
14
|
+
def initialize(url, debug: false)
|
|
15
15
|
@url = url
|
|
16
16
|
@socket = nil
|
|
17
17
|
@callbacks = {}
|
|
18
|
+
@debug = debug
|
|
19
|
+
@connected = false
|
|
18
20
|
end
|
|
19
21
|
|
|
20
22
|
def connect
|
|
21
|
-
|
|
22
|
-
|
|
23
|
+
debug_log "Connecting to: #{@url}"
|
|
24
|
+
|
|
25
|
+
begin
|
|
26
|
+
@socket = SocketIO::Client::Simple.connect(@url, transports: ['websocket'])
|
|
27
|
+
setup_handlers
|
|
28
|
+
debug_log "Socket.IO client created successfully"
|
|
29
|
+
rescue => e
|
|
30
|
+
debug_log "Connection failed: #{e.message}"
|
|
31
|
+
emit('error', { message: e.message, type: 'connection_error' })
|
|
32
|
+
raise e
|
|
33
|
+
end
|
|
23
34
|
end
|
|
24
35
|
|
|
25
36
|
def setup_handlers
|
|
26
37
|
@socket.on :connect do
|
|
38
|
+
@connected = true
|
|
39
|
+
debug_log "Socket.IO connected successfully"
|
|
27
40
|
emit('connected')
|
|
28
41
|
end
|
|
29
42
|
|
|
30
43
|
@socket.on :disconnect do
|
|
44
|
+
@connected = false
|
|
45
|
+
debug_log "Socket.IO disconnected"
|
|
31
46
|
emit('disconnected')
|
|
32
47
|
end
|
|
33
48
|
|
|
49
|
+
@socket.on :error do |error|
|
|
50
|
+
debug_log "Socket.IO error: #{error}"
|
|
51
|
+
emit('error', error)
|
|
52
|
+
end
|
|
53
|
+
|
|
34
54
|
@socket.on 'server-event' do |data|
|
|
55
|
+
debug_log "Received server-event: #{data}"
|
|
35
56
|
emit('message', data)
|
|
36
57
|
end
|
|
37
58
|
|
|
38
59
|
@socket.on 'realtimex_internal:subscription_succeeded' do |data|
|
|
60
|
+
debug_log "Subscription succeeded: #{data}"
|
|
39
61
|
emit('subscription_succeeded', data)
|
|
40
62
|
end
|
|
41
63
|
end
|
|
42
64
|
|
|
43
65
|
def send_subscribe(channel)
|
|
66
|
+
ensure_connected
|
|
67
|
+
debug_log "Subscribing to channel: #{channel}"
|
|
44
68
|
@socket.emit('realtimex:subscribe', { channel: channel })
|
|
45
69
|
end
|
|
46
70
|
|
|
47
71
|
def send_client_event(channel, event, data)
|
|
72
|
+
ensure_connected
|
|
73
|
+
debug_log "Sending client event: #{event} to #{channel}"
|
|
48
74
|
@socket.emit('client-event', {
|
|
49
75
|
channel: channel,
|
|
50
76
|
event: event,
|
|
@@ -53,6 +79,8 @@ class RealtimeX::Connection
|
|
|
53
79
|
end
|
|
54
80
|
|
|
55
81
|
def send_unsubscribe(channel)
|
|
82
|
+
ensure_connected
|
|
83
|
+
debug_log "Unsubscribing from channel: #{channel}"
|
|
56
84
|
@socket.emit('realtimex:unsubscribe', { channel: channel })
|
|
57
85
|
end
|
|
58
86
|
|
|
@@ -65,6 +93,24 @@ class RealtimeX::Connection
|
|
|
65
93
|
end
|
|
66
94
|
|
|
67
95
|
def disconnect
|
|
96
|
+
debug_log "Disconnecting..."
|
|
68
97
|
@socket&.disconnect
|
|
98
|
+
@connected = false
|
|
99
|
+
end
|
|
100
|
+
|
|
101
|
+
def connected?
|
|
102
|
+
@connected
|
|
103
|
+
end
|
|
104
|
+
|
|
105
|
+
private
|
|
106
|
+
|
|
107
|
+
def ensure_connected
|
|
108
|
+
unless @connected
|
|
109
|
+
raise "Not connected to RealtimeX. Call connect() first."
|
|
110
|
+
end
|
|
111
|
+
end
|
|
112
|
+
|
|
113
|
+
def debug_log(message)
|
|
114
|
+
puts "[RealtimeX] #{message}" if @debug
|
|
69
115
|
end
|
|
70
116
|
end
|
data/lib/realtimex/version.rb
CHANGED
data/lib/realtimex.rb
CHANGED
|
@@ -19,7 +19,7 @@ class RealtimeX
|
|
|
19
19
|
@channel_manager = ChannelManager.new
|
|
20
20
|
|
|
21
21
|
url = build_url
|
|
22
|
-
@connection = RealtimeX::Connection.new(url)
|
|
22
|
+
@connection = RealtimeX::Connection.new(url, debug: @options[:debug])
|
|
23
23
|
setup_connection_handlers
|
|
24
24
|
end
|
|
25
25
|
|
|
@@ -51,7 +51,8 @@ class RealtimeX
|
|
|
51
51
|
{
|
|
52
52
|
ws_host: 'localhost',
|
|
53
53
|
ws_port: 3001,
|
|
54
|
-
encrypted: false
|
|
54
|
+
encrypted: false,
|
|
55
|
+
debug: false
|
|
55
56
|
}
|
|
56
57
|
end
|
|
57
58
|
|
|
@@ -66,6 +67,10 @@ class RealtimeX
|
|
|
66
67
|
@connection.bind('message') do |data|
|
|
67
68
|
route_to_channel(data)
|
|
68
69
|
end
|
|
70
|
+
|
|
71
|
+
@connection.bind('error') do |error|
|
|
72
|
+
puts "[RealtimeX] Connection error: #{error}" if @options[:debug]
|
|
73
|
+
end
|
|
69
74
|
end
|
|
70
75
|
|
|
71
76
|
def route_to_channel(message)
|