Class: GRHttp::WSEvent

Inherits:
Object
  • Object
show all
Defined in:
lib/grhttp/ws_event.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(io, data = nil) ⇒ WSEvent

The initializer is called by the GRHttp server, setting the websocket's event data and the IO used for sending websocket data.


10
11
12
13
# File 'lib/grhttp/ws_event.rb', line 10

def initialize io, data = nil
	@io = io
	@data = data
end

Instance Attribute Details

#dataObject (readonly)

The websocket's event data.


7
8
9
# File 'lib/grhttp/ws_event.rb', line 7

def data
  @data
end

#ioObject (readonly)

the IO wrapper object used internally to send data. It's available for you mainly so that you could set data in the IO object's cache.


5
6
7
# File 'lib/grhttp/ws_event.rb', line 5

def io
  @io
end

Instance Method Details

#autoping(interval = 45) ⇒ Object

Starts auto-pinging every set interval (in seconds), until the websocket closes - this cannot be stopped once started.


81
82
83
84
# File 'lib/grhttp/ws_event.rb', line 81

def autoping interval = 45
	AUTOPING_PROC.call self, interval
	true
end

#autopong(interval = 45) ⇒ Object

Starts auto-ponging every set interval (in seconds), until the websocket closes - this cannot be stopped once started.


87
88
89
90
# File 'lib/grhttp/ws_event.rb', line 87

def autopong interval = 45
	AUTOPONG_PROC.call self, interval
	true
end

#broadcast(data) ⇒ Object

Broadcasts data to ALL websocket connection Handlers sharing the same process EXCEPT this websocket connection.

A handler must implement the `#on_broadcast(data)` to accept broadcasts and unicasts. The broadcast / unicast will be sent to the *handler* but NOT to the *client*.

Accepts only ONE data object - usually a Hash, Array, String or a JSON formatted object.

It is better to broadcast only data that would fit in a JSON string, as to allow easier multi-process / multi-machine scaling.

For inter-process broadcasts/unicasts, use this method in conjuncture with a Pub/Sub service such as Redis.


59
60
61
# File 'lib/grhttp/ws_event.rb', line 59

def broadcast data
	Base::WSHandler.broadcast data, self.io
end

#closeObject Also known as: disconnect

Closes the websocket connection.


29
30
31
32
# File 'lib/grhttp/ws_event.rb', line 29

def close
	@io.write( CLOSE_FRAME )
	@io.close
end

#closed?true, false

Returns true if the websocket connection is closed in both directions (calles the socket.closed? method).

Returns:

  • (true, false)

    returns true if the websocket connection is closed in both directions (calles the socket.closed? method).


35
36
37
# File 'lib/grhttp/ws_event.rb', line 35

def closed?
	@io.io.closed?
end

#pingObject

Sends a ping and returns he WSEvent object.


39
40
41
42
# File 'lib/grhttp/ws_event.rb', line 39

def ping
	@io.write PING_FRAME
	self
end

#pongObject

Sends a pong and returns he WSEvent object.


44
45
46
47
# File 'lib/grhttp/ws_event.rb', line 44

def pong
	@io.write PONG_FRAME
	self
end

#unicast(data, uuid) ⇒ Object

Broadcasts data to ONE websocket connection sharing the same process, as indicated by it's UUID.

A handler must implement the `#on_broadcast(data)` to accept broadcasts and unicasts. The broadcast / unicast will be sent to the *handler* but NOT to the *client*.

Accepts:

data

ONE data object - usually a Hash, Array, String or a JSON formatted object.

uuid

the websocket reciever's UUID.

It is better to broadcast only data that would fit in a JSON string, as to allow easier multi-process / multi-machine scaling.

For inter-process broadcasts/unicasts, use this method in conjuncture with a Pub/Sub service such as Redis.


76
77
78
# File 'lib/grhttp/ws_event.rb', line 76

def unicast data, uuid
	Base::WSHandler.unicast data, uuid
end

#uuidObject

Returns the websocket connection's UUID, used for unicasting.


16
17
18
# File 'lib/grhttp/ws_event.rb', line 16

def uuid
	io[:uuid] ||= SecureRandom.uuid
end

#write(data) ⇒ Object Also known as: send, <<

Encodes data according to the websocket standard and sends the data over the websocket connection.


21
22
23
24
25
# File 'lib/grhttp/ws_event.rb', line 21

def write data
	# should synchronize?
	# \@io.locker.synchronize { ... } 
	Base::WSHandler.send_data @io, data.to_s
end