Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions lib/socket.io-client-simple.rb
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
require 'socket.io-client-simple/version'
require 'socket.io-client-simple/error'
require 'socket.io-client-simple/client'
require 'socket.io-client-simple/byte_buffer'

module SocketIO
module Client
Expand Down
19 changes: 19 additions & 0 deletions lib/socket.io-client-simple/byte_buffer.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
module SocketIO
module Client
module Simple

def self.as_byte_buffer(data)
ByteBuffer.new(data)
end

class ByteBuffer
attr_accessor :buffer

def initialize(byte_array)
return unless byte_array.is_a?(Array)
@buffer = byte_array.unshift(4).pack('C*')
end
end
end
end
end
32 changes: 28 additions & 4 deletions lib/socket.io-client-simple/client.rb
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ def initialize(url, opts={})
@url = url
@opts = opts
@opts[:transport] = :websocket
@path = @opts.delete(:path) || "/socket.io"
@reconnecting = false
@state = :disconnect
@auto_reconnection = true
Expand Down Expand Up @@ -49,7 +50,7 @@ def initialize(url, opts={})
def connect
query = @opts.map{|k,v| URI.encode "#{k}=#{v}" }.join '&'
begin
@websocket = WebSocket::Client::Simple.connect "#{@url}/socket.io/?#{query}"
@websocket = WebSocket::Client::Simple.connect "#{@url}#{@path}/?#{query}"
rescue Errno::ECONNREFUSED => e
@state = :disconnect
@reconnecting = false
Expand Down Expand Up @@ -116,8 +117,9 @@ def open?
def emit(event_name, *data)
return unless open?
return unless @state == :connect
data.unshift event_name
@websocket.send "42#{data.to_json}"
data.each do |packet|
is_binary?(packet) ? send_binary(event_name, packet) : send_data(event_name, packet)
end
end

def disconnect
Expand All @@ -126,8 +128,30 @@ def disconnect
@state = :disconnect
end

end
private

def is_binary?(packet)
packet.is_a?(SocketIO::Client::Simple::ByteBuffer)
end

def send_data(event_name, *data)
data.unshift event_name
@websocket.send "42#{data.to_json}"
end

def send_binary(event_name, packet)
header = binary_header.unshift event_name
@websocket.send "451-#{header.to_json}"
@websocket.send packet.buffer, type: :binary
end

def binary_header
[{
_placeholder: true,
num: 0
}]
end
end
end
end
end
2 changes: 1 addition & 1 deletion lib/socket.io-client-simple/version.rb
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
module SocketIO
module Client
module Simple
VERSION = "1.2.1"
VERSION = "1.2.2"
end
end
end