realtimex 1.0.2 → 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 CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: cf536ab6d16acac8ada87c554d1b9697e09e604355b0e777e8256bf45237f795
4
- data.tar.gz: 7dd37fb6d906622fc2468eb2820a4410de5cefe2b93188704a7a92eb12c7ad4b
3
+ metadata.gz: 186c4a4f137225ab7ca45bfa90451ec86b5ec8fad21aeb61b8900bf43b7f5a6e
4
+ data.tar.gz: ace0043ea61e33a3ccffe10a30e879af118179c2757e8a48ee5cc5313bcaa1ac
5
5
  SHA512:
6
- metadata.gz: b8c31839ce117978ee6caa6d7b4d752d9f9b755b44aaeef2d0b5635a871e97611bc5fcf3a32ae7011f54dc7e2c49765d7647220b2830b5da1f8da71d636c65d0
7
- data.tar.gz: 92e1845f6d26f0128b18fa05f9e70b7e5ccc7995fff0444862db7cf43aea8207852ff2495461eead600e6545a3cfc95e562409b29f16b01a3c0bda78ea8afdf3
6
+ metadata.gz: 96e9b4348dd23ae60c4e5ff9e0b69826a56f22d6c56a3911ed4379b5b859d9d8c1c7992924abef71f09ed254dd431f8e75ed958bb116a7e178972a52634ceb31
7
+ data.tar.gz: 31de2c7549e275d9586649ac96d8a5468755a1c0508a7822f50338114b6e223a43b064441c86dce4b3f0bf7613a9aaff5f50f886ecc1b8ae63fa67815ce24b3c
@@ -1,40 +1,76 @@
1
+ require 'uri'
2
+ require 'cgi'
3
+
4
+ # Patch for Ruby 3.4 compatibility
5
+ module URI
6
+ def self.encode(str)
7
+ CGI.escape(str)
8
+ end
9
+ end unless URI.respond_to?(:encode)
10
+
1
11
  require 'socket.io-client-simple'
2
12
 
3
13
  class RealtimeX::Connection
4
- def initialize(url)
14
+ def initialize(url, debug: false)
5
15
  @url = url
6
16
  @socket = nil
7
17
  @callbacks = {}
18
+ @debug = debug
19
+ @connected = false
8
20
  end
9
21
 
10
22
  def connect
11
- @socket = SocketIO::Client::Simple.connect(@url, transports: ['websocket'])
12
- setup_handlers
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
13
34
  end
14
35
 
15
36
  def setup_handlers
16
37
  @socket.on :connect do
38
+ @connected = true
39
+ debug_log "Socket.IO connected successfully"
17
40
  emit('connected')
18
41
  end
19
42
 
20
43
  @socket.on :disconnect do
44
+ @connected = false
45
+ debug_log "Socket.IO disconnected"
21
46
  emit('disconnected')
22
47
  end
23
48
 
49
+ @socket.on :error do |error|
50
+ debug_log "Socket.IO error: #{error}"
51
+ emit('error', error)
52
+ end
53
+
24
54
  @socket.on 'server-event' do |data|
55
+ debug_log "Received server-event: #{data}"
25
56
  emit('message', data)
26
57
  end
27
58
 
28
59
  @socket.on 'realtimex_internal:subscription_succeeded' do |data|
60
+ debug_log "Subscription succeeded: #{data}"
29
61
  emit('subscription_succeeded', data)
30
62
  end
31
63
  end
32
64
 
33
65
  def send_subscribe(channel)
66
+ ensure_connected
67
+ debug_log "Subscribing to channel: #{channel}"
34
68
  @socket.emit('realtimex:subscribe', { channel: channel })
35
69
  end
36
70
 
37
71
  def send_client_event(channel, event, data)
72
+ ensure_connected
73
+ debug_log "Sending client event: #{event} to #{channel}"
38
74
  @socket.emit('client-event', {
39
75
  channel: channel,
40
76
  event: event,
@@ -43,6 +79,8 @@ class RealtimeX::Connection
43
79
  end
44
80
 
45
81
  def send_unsubscribe(channel)
82
+ ensure_connected
83
+ debug_log "Unsubscribing from channel: #{channel}"
46
84
  @socket.emit('realtimex:unsubscribe', { channel: channel })
47
85
  end
48
86
 
@@ -55,6 +93,24 @@ class RealtimeX::Connection
55
93
  end
56
94
 
57
95
  def disconnect
96
+ debug_log "Disconnecting..."
58
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
59
115
  end
60
116
  end
@@ -1,3 +1,3 @@
1
1
  class RealtimeX
2
- VERSION = "1.0.2"
2
+ VERSION = "1.0.4"
3
3
  end
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)
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: realtimex
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.0.2
4
+ version: 1.0.4
5
5
  platform: ruby
6
6
  authors:
7
7
  - RealtimeX Team