gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-02 23:24:20 25 4
gpt4 key购买 nike

我尝试在本地主机上运行 websocket 服务器并使用 ngrok 将其转发到网络。但想不出怎么办。这些是来自 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"\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

我在这里做错了什么?

最佳答案

我尝试使用

ngrok http 8091

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

关于websocket - 如何使用 ngrok 在本地主机中转发 websocket 服务器,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22830330/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com