Sha256: eef192700e157740c9d7c1d665abbe5b18c422abee863c8810ff28e632835fea

Contents?: true

Size: 1.42 KB

Versions: 3

Compression:

Stored size: 1.42 KB

Contents

require 'websocket-eventmachine-server'
module Smash
  module CloudPowers
    module Synapse
      module WebSocServer
        def create_websoc_server(opts = {})
          channel = opts[:channel] || EM::Channel.new
          Thread.new do
            EM.run do
              WebSocket::EventMachine::Server.start(:host => opts[:host], :port => opts[:port]) do |ws|
                sid = nil

                open_callback = opts[:on_open] || Proc.new do
                  puts "Client connected"
                  sid = channel.subscribe { |msg| ws.send msg }
                end
                ws.onopen &open_callback

                on_message_callback = opts[:on_message] || Proc.new do |msg, type|
                  @current_websocket_message = msg
                end
                ws.onmessage &on_message_callback

                on_error_callback = opts[:on_error] || Proc.new do |error|
                  puts "Error occured: #{error}"
                end
                ws.onerror &on_error_callback

                on_close_callback = opts[:on_close] || Proc.new do
                  puts "Client disconnected"
                  channel.unsubscribe(sid) unless channel.nil?
                end
                ws.onclose &on_close_callback
              end
            end
          end
          channel
        end

        def broadcast_message(channel, msg)
          channel.push msg
        end
      end
    end
  end
end

Version data entries

3 entries across 3 versions & 1 rubygems

Version Path
cloud_powers-0.2.7.23 lib/cloud_powers/synapse/websocket/websocserver.rb
cloud_powers-0.2.7.22 lib/cloud_powers/synapse/websocket/websocserver.rb
cloud_powers-0.2.7.21 lib/cloud_powers/synapse/websocket/websocserver.rb