小编AEl*_*Ela的帖子

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

我试图在本地主机上运行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 = …
Run Code Online (Sandbox Code Playgroud)

localhost websocket autobahn ngrok

5
推荐指数
1
解决办法
5579
查看次数

标签 统计

autobahn ×1

localhost ×1

ngrok ×1

websocket ×1