WebSocketChatServer

A wrapper class for em-websocket (https://github.com/igrigorik/em-websocket) implementing a custom chat server protocol.

Installation

Add this line to your application's Gemfile:

gem 'web_socket_chat_server'

And then execute:

$ bundle

Or install it yourself as:

$ gem install web_socket_chat_server

Documentation

For better understanding, check the docs at http://abulewis.com/ws/doc/WebSocketChatServer/ChatServer.html

Usage

An example on how to run the server:

require "web_socket_chat_server.rb"

admin1 = WebSocketChatServer::ChatUser.new(:username => "admin", :password => "secret")
admin2 = WebSocketChatServer::ChatUser.new(:username => "admin2", :password => "secret2")
admins = []
admins << admin1
admins << admin2

chat = WebSocketChatServer::ChatServer.new(:host=> "0.0.0.0", :port => "8080", :admins => admins)

chat.start_server do |response|
puts response
end

['TERM', 'INT'].each do |signal|
trap(signal){ 
chat.stop_server()
}
end

puts "Running..........."
wait = gets

The code above creates an array consisting of two admins (ChatUser objects) and passes them to initialize(). The server will listen on IP 0.0.0.0 and the port 8080 (Required parameters).

By running the code above, you do not need to do any other things. The class takes care of the of itself by handling the connections and the logic of the server’s custom protocol. You still though have the opportunity to print out the commands generated by the server by passing a block to the start_server() function as it has been done above.

On the client side, use the HTML WebSocket API to connect to the server. An example on a fully working Chat client will be attached to this repo (CHAT_CLIENT_EXAMPLE).

To explain how to connect to the server briefly:

You have to pass the query parameters username & password to the socket client when connecting.

On successful connection, you can send a JSON hash of the format ”…”, data: ”…

Available commands are:

”chat_message”, data: ”your message”

”private_message”, data: {to_user: ”chuck_norris”, message: ”Private message”}

”ban_user”, data: ”chuck_norris”

The server will respond with a JSON hash of the format ”…”, data: ”…”, information: ”…”

Available responses are:

”failed_connection”, data: nil, information: ”Some details”

”successful_connection”, data: array_of_connected_users, information: ”Some details”

”new_connection”, data: username, information: ”A new connected user”

”chat_message”, data: {from_user: ”chuck_norris”, message: ”The message”, information: ”Some details.”}

”ban_user”, data: TRUE or FALSE, information: ”Some details.”

”private_message”, data: {from_user: ”chuck_norris”, message: ”The message”, information: ”Some details.”}

”system_information”, data: ”Issue”, information: ”Some details.”

”user_disconnected”, data: ”username”, information: ”Some details.”