Class: OverSIP::WebSocket::WsApp

Inherits:
Object
  • Object
show all
Includes:
Logger
Defined in:
lib/oversip/websocket/ws_app.rb

Direct Known Subclasses

WsAutobahnApp, WsSipApp

Constant Summary

Constants included from Logger

Logger::SYSLOG_POSIXMQ_MAPPING

Class Method Summary collapse

Instance Method Summary collapse

Methods included from Logger

close, #fatal, fg_system_msg2str, init_logger_mq, load_methods, #log_id, syslog_system_msg2str, syslog_user_msg2str

Constructor Details

#initialize(connection, ws_framing) ⇒ WsApp

Returns a new instance of WsApp.



14
15
16
17
18
19
20
21
# File 'lib/oversip/websocket/ws_app.rb', line 14

def initialize connection, ws_framing
  @connection = connection
  @ws_framing = ws_framing
  @ws_message = ::IO::Buffer.new

  # Mantain WebSocket keepalive.
  @ws_framing.do_keep_alive @@ws_keepalive_interval  if @@ws_keepalive_interval
end

Class Method Details

.class_initObject



8
9
10
11
# File 'lib/oversip/websocket/ws_app.rb', line 8

def self.class_init
  @@max_message_size = ::OverSIP.configuration[:websocket][:max_ws_message_size]
  @@ws_keepalive_interval = ::OverSIP.configuration[:websocket][:ws_keepalive_interval]
end

Instance Method Details

#close_connection(status = nil, reason = nil) ⇒ Object



24
25
26
# File 'lib/oversip/websocket/ws_app.rb', line 24

def close_connection status=nil, reason=nil
  @ws_framing.send_close_frame status, reason
end

#message_done(type) ⇒ Object



42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
# File 'lib/oversip/websocket/ws_app.rb', line 42

def message_done type
  log_system_debug "received WS message: length=#{@ws_message.size}"  if $oversip_debug

  case type

  when :text
    ws_message = @ws_message.to_str.force_encoding ::Encoding::UTF_8
    process_text_message ws_message

  when :binary
    process_binary_message @ws_message.to_str  # As IO::Buffer#to_str always generates Encoding::BINARY.
  end

  @ws_message.clear
  true
end

#process_binary_message(ws_message) ⇒ Object



69
70
# File 'lib/oversip/websocket/ws_app.rb', line 69

def process_binary_message ws_message
end

#process_text_message(ws_message) ⇒ Object

Methods to be overriden by child classes.



66
67
# File 'lib/oversip/websocket/ws_app.rb', line 66

def process_text_message ws_message
end

#receive_payload_data(payload_data) ⇒ Object



29
30
31
32
33
34
35
36
37
38
39
# File 'lib/oversip/websocket/ws_app.rb', line 29

def receive_payload_data payload_data
  # payload_data is always Encoding::BINARY so also @ws_message.to_str.
  @ws_message << payload_data

  # Check max message size.
  if @ws_message.size > @@max_message_size
    close_connection 1009, "message too big"
    return false
  end
  true
end

#tcp_closedObject



60
61
62
# File 'lib/oversip/websocket/ws_app.rb', line 60

def tcp_closed
  nil
end