Class: WebSocketChatServer::ChatServer
- Inherits:
-
Object
- Object
- WebSocketChatServer::ChatServer
- Defined in:
- lib/web_socket_chat_server.rb
Overview
A wrapper class for em-websocket (github.com/igrigorik/em-websocket) implementing a custom chat server protocol.
Instance Attribute Summary collapse
-
#admins ⇒ Object
readonly
An array of ChatUser objects representing the admins of the chat.
-
#banned_usernames ⇒ Object
An array of string containing the banned usernames.
-
#connected_users ⇒ Object
readonly
A hash containing the connected users.
-
#host ⇒ Object
readonly
The host address.
-
#max_connections ⇒ Object
The max number of allowed client connections.
-
#port ⇒ Object
readonly
The socket port to be used.
Instance Method Summary collapse
-
#initialize(args = {:host => "0.0.0.0", :port => 8080}) ⇒ ChatServer
constructor
The class initializer ==== args *
:server(Required) - The host of the chat server to run on. -
#start_server ⇒ Object
To start the server, call this method.
-
#started? ⇒ Boolean
Returns true if the server is started, otherwise false.
-
#stop_server ⇒ Object
To stop the server, call this method.
Constructor Details
#initialize(args = {:host => "0.0.0.0", :port => 8080}) ⇒ ChatServer
The class initializer
args
-
:server(Required) - The host of the chat server to run on. -
:port(Required) - The port of the server. -
:max_connections(Optional) - The max client connections allowed. If not passed, the default is 100. -
:secure(Optional) - In case of using Secure Servers, pass true as allowed by em-websocket (github.com/igrigorik/em-websocket#secure-server). -
:tls-options(Optional) - The TLS options as allowed by em-websocket (github.com/igrigorik/em-websocket#secure-server). -
:secure_proxy(Optional) - When ruinng behind a SSL proxy (github.com/igrigorik/em-websocket#running-behind-an-ssl-proxyterminator-like-stunnel). -
:admins(Optional) - In order to have admins (for banning users). Pass an array of ChatUser objects to this key. The ChatUser object contains of username and password attributes. The username must consist of at least 3 alphanumeric characters. The password must at least consist of 6 alphanumeric characters. -
:allowed_origin(Optional) - Allow connections only from the passed URI. The URI should be the domain and/or port (depending on the server setup), in which your application is operating.
37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 |
# File 'lib/web_socket_chat_server.rb', line 37 def initialize(args = {:host => "0.0.0.0", :port => 8080}) @server_started = false raise ArgumentError, "The :host parameter is required" unless args.has_key?(:host) raise ArgumentError, "The :port parameter is required" unless args.has_key?(:port) raise ArgumentError, "The port value is not valid" unless valid_port?( args[:port]) @origin = "" @origin = args[:allowed_origin] if args.has_key?(:allowed_origin) @max_connections = args[:max_connections].to_s.to_i @max_connections = 100 if @max_connections <= 0 @host = args[:host] @port = args[:port] @admins = [] @banned_usernames = [] @connected_users = Hash.new @server_options = Hash.new @server_options[:host] = @host @server_options[:port] = @port.to_s.to_i @server_options[:secure] = args[:secure] if args.has_key?(:secure) @server_options[:tls_options] = args[:tls_options] if args.has_key?(:tls_options) @server_options[:secure_proxy] = args[:secure_proxy] if args.has_key?(:secure_proxy) if args.has_key?(:admins) if args[:admins].class == Array args[:admins].each { |chat_user| if chat_user.class == ChatUser raise RuntimeError, "The admin's username has to be at least 3 alphanumeric characters, and the password at least 6 alphanumeric characters" unless user_credentials_valid?(chat_user.username, chat_user.password) @admins << chat_user unless @admins.include?(chat_user) end } end end end |
Instance Attribute Details
#admins ⇒ Object (readonly)
An array of ChatUser objects representing the admins of the chat.
16 17 18 |
# File 'lib/web_socket_chat_server.rb', line 16 def admins @admins end |
#banned_usernames ⇒ Object
An array of string containing the banned usernames.
10 11 12 |
# File 'lib/web_socket_chat_server.rb', line 10 def banned_usernames @banned_usernames end |
#connected_users ⇒ Object (readonly)
A hash containing the connected users. The key contains of the user’s username and the value is a ChatUser object.
19 20 21 |
# File 'lib/web_socket_chat_server.rb', line 19 def connected_users @connected_users end |
#host ⇒ Object (readonly)
The host address.
22 23 24 |
# File 'lib/web_socket_chat_server.rb', line 22 def host @host end |
#max_connections ⇒ Object
The max number of allowed client connections.
13 14 15 |
# File 'lib/web_socket_chat_server.rb', line 13 def max_connections @max_connections end |
#port ⇒ Object (readonly)
The socket port to be used.
25 26 27 |
# File 'lib/web_socket_chat_server.rb', line 25 def port @port end |
Instance Method Details
#start_server ⇒ Object
To start the server, call this method. It will return true if successful, false if it is already started. Otherwise, RuntimeError will be raised on errors. If giving a block, A hash “…”, data: “…”, information: “…” is yield.
77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 |
# File 'lib/web_socket_chat_server.rb', line 77 def start_server() return false if @server_started begin Thread.new {run_server() do |response| yield response if block_given? end } rescue Exception => e @server_started = false raise RuntimeError, e. end until @server_started next end @server_started end |
#started? ⇒ Boolean
Returns true if the server is started, otherwise false.
111 112 113 |
# File 'lib/web_socket_chat_server.rb', line 111 def started?() @server_started end |
#stop_server ⇒ Object
To stop the server, call this method. True if the server is stopped, false if it is already stopped. Otherwise, RuntimeError is raised on errors.
98 99 100 101 102 103 104 105 106 107 108 |
# File 'lib/web_socket_chat_server.rb', line 98 def stop_server() return false unless @server_started @server_started = false @connected_users.clear if @connected_users.count > 0 begin EM::WebSocket.stop() rescue Exception => e raise RuntimeError, e. end true end |