Sha256: 00d132ccd83e58178705b39fde30e078829d193a420f254f2c34bd8d7537aeb1

Contents?: true

Size: 1.31 KB

Versions: 2

Compression:

Stored size: 1.31 KB

Contents

# encoding=utf-8
require 'eventmachine'
require 'websocket-eventmachine-server'
require 'faye/websocket'
module OPQ
  class WsServer
    def initialize(observer)
      @observer = observer
      self.start
    end
    def start
      EM.run {
        ws = Faye::WebSocket::Client.new('ws://'+$api_url+':'+$http_port+'/ws')
        ws.on :open do |event|
          puts "[WS] 连接已建立"
        end

        ws.on :message do |event|
          puts "[WS] 收到数据-->".force_encoding('UTF-8')+ "#{event.data}".force_encoding('UTF-8')
          @observer.on_message_received(event.data)
        end

        ws.on :close do |event|
          puts "[WS] 连接已断开"
          ws = nil
        end
      }
        # EM.run do
        #   WebSocket::EventMachine::Server.start(:host => "0.0.0.0", :port => @port, :mode => :async) do |ws|
        #     ws.onopen do
        #       puts "[WS] 连接已建立"
        #     end
        #     ws.onmessage do |msg, type|
        #       puts "[WS] 收到数据-->".force_encoding('UTF-8')+ "#{msg}".force_encoding('UTF-8')
        #       @observer.on_message_received(msg)
        #     end
        #
        #     ws.onclose do
        #       puts "[WS] 连接已断开"
        #     end
        #   end
        # end
    end
  end

end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
opqr-0.1.4 lib/opqr/wsserver.rb
opqr-0.1.3 lib/opqr/wsserver.rb