11
votes

I' trying to run a websocket server on local host and forward it to web using ngrok. But couldn't figure it how. These are the original code's from AutobahnPython git repository https://github.com/tavendo/AutobahnPython.

Server code:

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()

Client Code:

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()

This is the changed code:

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()

This is the ngrok command: ./ngrok -proto=tcp 9000

What am I doing wrong here?

1

1 Answers

10
votes

I tried using

ngrok http 8091

(where 8091 is the port where my WebsocketServer is running using Racthet IO) and it works. I can still connect to the Websocket replacing the http with ws on the connection.