Class: Rakie::WebsocketServer

Inherits:
Websocket show all
Defined in:
lib/rakie/websocket_server.rb

Instance Attribute Summary

Attributes inherited from Websocket

#channel, #client_side, #delegate

Instance Method Summary collapse

Constructor Details

#initialize(delegate = nil, http_server = nil) ⇒ WebsocketServer

Returns a new instance of WebsocketServer.

Parameters:



4
5
6
7
8
9
10
11
12
13
14
15
16
# File 'lib/rakie/websocket_server.rb', line 4

def initialize(delegate=nil, http_server=nil)
  @delegate = delegate

  if http_server == nil
    http_server = HttpServer.new('127.0.0.1', 10086, self)
  end

  http_server.opt[:websocket_delegate] = self
  @channel = http_server.channel

  # @type [Array<WebsocketClient>]
  @clients = {}
end

Instance Method Details

#clientsObject



76
77
78
# File 'lib/rakie/websocket_server.rb', line 76

def clients
  @clients.values
end

#closeObject



74
# File 'lib/rakie/websocket_server.rb', line 74

def close; end

#on_accept(channel) ⇒ Object



18
19
20
21
22
23
24
25
26
27
28
29
30
31
# File 'lib/rakie/websocket_server.rb', line 18

def on_accept(channel)
  ws_client = Websocket.new(@delegate, channel)
  ws_client.client_side = false

  channel.delegate = self

  if @delegate
    @delegate.on_connect(ws_client)
    return
  end

  @clients[channel] = ws_client
  Log.debug("Rakie::WebsocketServer accept client: #{ws_client}")
end

#on_close(channel) ⇒ Object



64
65
66
67
68
69
70
# File 'lib/rakie/websocket_server.rb', line 64

def on_close(channel)
  client = @clients[channel]

  if client
    client.on_close(channel)
  end
end

#on_recv(channel, data) ⇒ Object



48
49
50
51
52
53
54
# File 'lib/rakie/websocket_server.rb', line 48

def on_recv(channel, data)
  client = @clients[channel]

  if client
    client.on_recv(channel, data)
  end
end

#on_send(channel) ⇒ Object



56
57
58
59
60
61
62
# File 'lib/rakie/websocket_server.rb', line 56

def on_send(channel)
  client = @clients[channel]

  if client
    client.on_send(channel)
  end
end

#send(message, is_binary = false) ⇒ Object



72
# File 'lib/rakie/websocket_server.rb', line 72

def send(message, is_binary=false); end

#upgrade(request, response) ⇒ Object

Returns bool.

Parameters:

Returns:

  • bool



36
37
38
39
40
41
42
43
44
45
46
# File 'lib/rakie/websocket_server.rb', line 36

def upgrade(request, response)
  if websocket_key = request.headers["sec-websocket-key"]
    digest_key = Digest::SHA1.base64digest(websocket_key + '258EAFA5-E914-47DA-95CA-C5AB0DC85B11')

    response.head.status = 101
    response.head.message = 'Switching Protocols'
    response.headers["connection"] = "upgrade"
    response.headers["upgrade"] = "websocket"
    response.headers["sec-websocket-accept"] = digest_key
  end
end