Sha256: 24dced874182035d44ae026be9fb8fa09e76654fd118c03fa6fbabe7be1f2e9d

Contents?: true

Size: 1.04 KB

Versions: 2

Compression:

Stored size: 1.04 KB

Contents

#require_relative "moonage/server"
require "websocket/eventmachine/server"

#TODO: ?
EM.epoll

module Moonrelay::Server
  def self.run!
    EM.run do
      $clients = {}
      $channels = {}

      WebSocket::EventMachine::Server.start(host: "0.0.0.0", port: 8080) do |ws|
        ws.onopen do |handshake|
          $channels[handshake.path] ||= {}
          $channels[handshake.path][ws.object_id] = ws
        end

        ws.onmessage do |msg, type|
          handshake = ws.instance_eval("@handshake")

          delivered = false
          $channels[handshake.path].each do |object_id, subscriber|
            unless subscriber == ws
              delivered = true
              p [type, msg]
              subscriber.send msg, type: type
            end
          end

          unless delivered
            ws.close
          end
        end

        ws.onclose do
          handshake = ws.instance_eval("@handshake")
          #$channels[handshake.path].delete ws.object_id
        end
      end

      puts "🌗 listening at 0.0.0.0:8080"
    end
  end
end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
moonrelay-0.2.1 lib/moonrelay/server.rb
moonrelay-0.2.0 lib/moonrelay/server.rb