Class: Mooncell::Protocol::WebSocket::Server Private

Inherits:
Object
  • Object
show all
Defined in:
lib/mooncell/protocol/websocket/server.rb

Overview

This class is part of a private API. You should avoid using this class if possible, as it may be removed or be changed in the future.

WebSocket Server implement

Since:

  • 0.1.0

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(app, env) ⇒ Server

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Create WebSocket Server

Since:

  • 0.1.0



22
23
24
25
26
27
28
29
# File 'lib/mooncell/protocol/websocket/server.rb', line 22

def initialize(app, env)
  @ws = Faye::WebSocket.new(env)
  @app = app

  @ws.on :open, method(:open)
  @ws.on :message, method(:message)
  @ws.on :close, method(:close)
end

Instance Attribute Details

#appObject (readonly)

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Since:

  • 0.1.0



16
17
18
# File 'lib/mooncell/protocol/websocket/server.rb', line 16

def app
  @app
end

Instance Method Details

#close(_event) ⇒ Object

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

On WebSocket close

Since:

  • 0.1.0



64
65
66
# File 'lib/mooncell/protocol/websocket/server.rb', line 64

def close(_event)
  app.pool.delete(@conn)
end

#message(event) ⇒ Object

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

On WebSocket receive data

Since:

  • 0.1.0



51
52
53
54
55
56
57
58
# File 'lib/mooncell/protocol/websocket/server.rb', line 51

def message(event)
  # TODO: Support customize serializer
  params = JSON.parse(event.data)
  app.router.call(@conn, params)
rescue JSON::ParserError
  # TODO: Handling error
  write(error: -1)
end

#open(_event) ⇒ Object

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

On WebSocket open

Since:

  • 0.1.0



43
44
45
# File 'lib/mooncell/protocol/websocket/server.rb', line 43

def open(_event)
  @conn = Mooncell::Connection.new(self, app)
end

#rack_responseObject

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Rack Response

Since:

  • 0.1.0



35
36
37
# File 'lib/mooncell/protocol/websocket/server.rb', line 35

def rack_response
  @ws.rack_response
end

#write(data) ⇒ Object

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Writable API

Since:

  • 0.1.0



72
73
74
# File 'lib/mooncell/protocol/websocket/server.rb', line 72

def write(data)
  @ws.send(data.to_json)
end