如何使用 ngrok 在本地主机中转发 websocket 服务器



我' 尝试在本地主机上运行 websocket 服务器并使用 ngrok 将其转发到 web。但想不通怎么做。这些是来自AutobahnPython git存储库 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"x00x01x03x04", 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"x00x01x03x04", 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

我在这里做错了什么?

我尝试使用

ngrok http 8091

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

尝试使用以下命令:

ngrok tcp 8091

它对我来说非常有效。

相关内容

  • 没有找到相关文章

最新更新