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(host: '127.0.0.1', port: 10086, 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
17
18
19
# File 'lib/rakie/websocket_server.rb', line 4

def initialize(host: '127.0.0.1', port: 10086, delegate: nil, http_server: nil)
  @delegate = delegate

  if http_server == nil
    http_server = HttpServer.new(host: host, port: port)
  end

  @host = http_server.host
  @port = http_server.port

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

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

Instance Method Details

#clientsObject



82
83
84
# File 'lib/rakie/websocket_server.rb', line 82

def clients
  @clients.values
end

#closeObject



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

def close; end

#on_accept(channel) ⇒ Object



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

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)
  end

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

#on_close(channel) ⇒ Object



68
69
70
71
72
73
74
75
76
# File 'lib/rakie/websocket_server.rb', line 68

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

  if client
    client.on_close(channel)
  end

  @clients.delete(channel)
end

#on_recv(channel, data) ⇒ Object



50
51
52
53
54
55
56
57
58
# File 'lib/rakie/websocket_server.rb', line 50

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

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

  return data.length
end

#on_send(channel) ⇒ Object



60
61
62
63
64
65
66
# File 'lib/rakie/websocket_server.rb', line 60

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

  if client
    client.on_send(channel)
  end
end

#send(message, is_binary = false) ⇒ Object



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

def send(message, is_binary=false); end

#upgrade(request, response) ⇒ Object

Returns bool.

Parameters:

Returns:

  • bool



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

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