如何使用ngrok在localhost中转发websocket服务器

时间:2014-04-03 07:10:47

标签: websocket localhost autobahn ngrok

我试图在本地主机上运行websocket服务器并使用ngrok将其转发到web。但无法弄清楚如何。这些是来自AutobahnPython git repository https://github.com/tavendo/AutobahnPython的原始代码。

服务器代码:

from autobahn.twisted.websocket import WebSocketServerProtocol, \
                                       WebSocketServerFactory


class MyServerProtocol(WebSocketServerProtocol):

   def onConnect(self, request):
      print("Client connecting: {0}".format(request.peer))

   def onOpen(self):
      print("WebSocket connection open.")

   def onMessage(self, payload, isBinary):
      if isBinary:
         print("Binary message received: {0} bytes".format(len(payload)))
      else:
         print("Text message received: {0}".format(payload.decode('utf8')))

      ## echo back message verbatim
      self.sendMessage(payload, isBinary)

   def onClose(self, wasClean, code, reason):
      print("WebSocket connection closed: {0}".format(reason))



if __name__ == '__main__':

   import sys

   from twisted.python import log
   from twisted.internet import reactor

   log.startLogging(sys.stdout)

   factory = WebSocketServerFactory("ws://localhost:9000", debug = False)
   factory.protocol = MyServerProtocol

   reactor.listenTCP(9000, factory)
   reactor.run()

客户代码:

from autobahn.twisted.websocket import WebSocketClientProtocol, \
                                       WebSocketClientFactory



class MyClientProtocol(WebSocketClientProtocol):

   def onConnect(self, response):
      print("Server connected: {0}".format(response.peer))

   def onOpen(self):
      print("WebSocket connection open.")

      def hello():
         self.sendMessage(u"Hello, world!".encode('utf8'))
         self.sendMessage(b"\x00\x01\x03\x04", isBinary = True)
         self.factory.reactor.callLater(1, hello)

      ## start sending messages every second ..
      hello()

   def onMessage(self, payload, isBinary):
      if isBinary:
         print("Binary message received: {0} bytes".format(len(payload)))
      else:
         print("Text message received: {0}".format(payload.decode('utf8')))

   def onClose(self, wasClean, code, reason):
      print("WebSocket connection closed: {0}".format(reason))



if __name__ == '__main__':

   import sys

   from twisted.python import log
   from twisted.internet import reactor

   log.startLogging(sys.stdout)

   factory = WebSocketClientFactory("ws://localhost:9000", debug = False)
   factory.protocol = MyClientProtocol

   reactor.connectTCP("127.0.0.1", 9000, factory)
   reactor.run()

这是更改后的代码:

from autobahn.twisted.websocket import WebSocketClientProtocol, \
                                       WebSocketClientFactory



class MyClientProtocol(WebSocketClientProtocol):

   def onConnect(self, response):
      print("Server connected: {0}".format(response.peer))

   def onOpen(self):
      print("WebSocket connection open.")

      def hello():
         self.sendMessage(u"Hello, world!".encode('utf8'))
         self.sendMessage(b"\x00\x01\x03\x04", isBinary = True)
         self.factory.reactor.callLater(1, hello)

      ## start sending messages every second ..
      hello()

   def onMessage(self, payload, isBinary):
      if isBinary:
         print("Binary message received: {0} bytes".format(len(payload)))
      else:
         print("Text message received: {0}".format(payload.decode('utf8')))

   def onClose(self, wasClean, code, reason):
      print("WebSocket connection closed: {0}".format(reason))



if __name__ == '__main__':

   import sys

   from twisted.python import log
   from twisted.internet import reactor

   log.startLogging(sys.stdout)

   #~ factory = WebSocketClientFactory("ws://localhost:9000", debug = False)
   factory = WebSocketClientFactory("ws://ngrok.com:xxxxx", debug = False)

   factory.protocol = MyClientProtocol

   reactor.connectTCP("ws://ngrok.com", xxxxx, factory)
   reactor.run()

这是ngrok命令: ./ ngrok -proto = tcp 9000

我在这里做错了什么?

1 个答案:

答案 0 :(得分:6)

我尝试使用

ngrok http 8091

(其中8091是使用Racthet IO运行我的WebsocketServer的端口)并且它可以工作。我仍然可以连接到Websocket,用连接上的http替换ws