用websocket包裹插座

时间:2012-12-20 10:16:09

标签: webserver websocket ada

是否可以使用带有websockets的web服务器作为另一台服务器的包装器,将消息从“真实服务器”传递到Web客户端并返回?

我很好奇,因为我有一个用Ada编写的游戏服务器,它有一个与操作系统相关的客户端。我想将此客户端交换为基于Javascript的webclient,以便可以在普通浏览器中播放游戏。可以做些什么?

5 个答案:

答案 0 :(得分:3)

这是websockify的目的。它旨在桥接WebSocket客户端和常规TCP服务器。它是作为noVNC的一部分创建的,这是一个可以连接到普通VNC服务器的HTML5 VNC应用程序。但是,websockify是通用的,现在有许多其他项目使用它。

免责声明:我创建了websockify和noVNC。

答案 1 :(得分:2)

您可以使用AWS轻松完成此任务:

http://libre.adacore.com/tools/aws/

AWS支持websockets,您可以利用它的优秀套接字(AWS.Net)软件包来获得正常的套接字支持。

答案 2 :(得分:1)

Websockets与某些人认为的相反,不是纯粹的套接字。原始数据由websocket协议封装和屏蔽,该协议尚未得到广泛支持。这意味着一个不是为它设计的应用程序,你无法通过网络套接字直接与它通信。

如果您的应用程序使用基于普通套接字的协议,并且您希望使用websockets与其进行通信,则有两种选择。

您可以使用websocket网关对websocket流量进行解包/打包,并将其作为纯套接字流量转发给应用程序。这样做的好处是您无需修改​​应用程序,但它的缺点是它还会掩盖客户端的真实IP地址,这可能会对某些应用程序产生影响,也可能不会出现问题。

或者直接在应用程序中实现websocket。这可以通过服务器监听两个不同的端口来完成,一个用于正常连接,另一个用于websocket连接。通过websocket-port接收或发送的任何数据都是在发送/接收之前通过您的websocket实现发送的,否则由相同的例程处理。

答案 3 :(得分:1)

Kaazing HTML5 Gateway是将基于TCP的协议引入Web客户端的绝佳方式。 Kaazing网关基本上使您的协议在TCP之上运行并将其转换为WebSocket,以便您可以在客户端访问协议。您仍然需要为后端使用的协议编写JavaScript协议库。但是如果你可以在TCP之上使用协议,那么使用JavaScript就不难了。

答案 4 :(得分:0)

我在ruby中使用以下代码来封装我的套接字。代码改编自em-websocket-proxy。我的项目可能有一些细节,但通常切换remote_host和remote_port并连接到localhost:3000应该通过websocket为你的服务器设置一个新的连接。

require 'rubygems'
require 'em-websocket'
require 'sinatra/base'
require 'thin'
require 'haml'
require 'socket'


class App < Sinatra::Base
    get '/' do
        haml :index
    end
end

class ServerConnection < EventMachine::Connection

  def initialize(input, output)
    super
    @input = input
    @output = output
    @input_sid = @input.subscribe { |msg| send_data msg+ "\n" }
  end

  def receive_data(msg)
    @output.push(msg)
  end

  def unbind
    @input.unsubscribe(@input_sid)
  end

end

# Configuration of server
options = {:remote_host => 'your-server', :remote_port => 4000}

EventMachine.run do   

  EventMachine::WebSocket.start(:host => '0.0.0.0', :port => 8080) do |ws|
    ws.onopen {
      output = EM::Channel.new
      input = EM::Channel.new

      output_sid = output.subscribe { |msg| ws.send msg; }

      EventMachine::connect options[:remote_host], options[:remote_port], ServerConnection, input, output

      ws.onmessage { |msg| input.push(msg)}

      ws.onclose {
        output.unsubscribe(output_sid)
      }
    }
  end

  App.run!({:port => 3000})   

end

享受!并询问您是否有疑问。

相关问题