如何正确连接WebSocketClient?

sq1bmfud  于 2022-11-11  发布在  其他
关注(0)|答案(1)|浏览(165)

我的godot客户端设置如下:

tool
extends Node2D

var _client = WebSocketClient.new()
var err;

func _init():
    _client.connect("connection_closed", self, "_closed")
    _client.connect("connection_error", self, "_closed")
    _client.connect("connection_established", self, "_connected")
    _client.connect("data_received", self, "_on_data")

    err = _client.connect_to_url("ws://127.0.0.1:6000")
    print("err=",err)
    if err != OK:
        print("Unable to connect")
        set_process(false)

func _closed(was_clean = false):
    print("Closed, clean: ", was_clean)
    #set_process(false)

func _connected(proto = ""):
    print("Connected with protocol: ", proto)
    _client.get_peer(1).put_packet("Hello there".to_utf8())

func _on_data():
    print("Got data from server: ", _client.get_peer(1).get_packet().get_string_from_utf8())

func _process(delta):
    _client.poll()

this示例所示
我的python端服务器如下所示:

from socket import *
s= socket(AF_INET, SOCK_STREAM)
s.setsockopt(SOL_SOCKET,SO_REUSEADDR, 1)
s.bind(("127.0.0.1",6000))

while True:
    print("Waiting for connection...")
    try:
        s.listen()
        conn, addr = s.accept()
        print("Connection Established!")

        while True:
            data = conn.recv(1024).decode()
            if not data:
                break
            print("Sender Says :",data)
            conn.send("General Kenobi!".encode())

    except error:
        print("Connection Terminated! Restarting...")

但每次我尝试它都会出错
条件“!is_connected_to_host()”为真。返回值:未通过
我不明白我做错了什么?

von4xj4u

von4xj4u1#

我得到了WebSocket的工作似乎你的python代码只是一个tcp服务器到这里有一个更好的了解websocket(Differences between TCP sockets and web sockets, one more time)我也使用python版本3.10.4.和Godot 3.5的最新版本。也运行服务器第一。
pip安装简单网络套接字服务器

GODOT客户端代码

extends Control

var _client = WebSocketClient.new()
var err;

func _init():
    _client.connect("connection_closed", self, "_closed")
    _client.connect("connection_error", self, "_closed")
    _client.connect("connection_established", self, "_connected")
    _client.connect("data_received", self, "_on_data")

    err = _client.connect_to_url("ws://127.0.0.1:8000")
    print("err=",err)
    if err != OK:
        print("Unable to connect")
        set_process(false)

func _closed(was_clean = false):
    print("Closed, clean: ", was_clean)
    #set_process(false)

func _connected(proto = ""):
    print("Connected with protocol: ", proto)
    _client.get_peer(1).put_packet("Hello there".to_utf8())

func _on_data():
    print("Got data from server: ", _client.get_peer(1).get_packet().get_string_from_utf8())

func _process(delta):
    _client.poll()

Python WebSocket服务器

from simple_websocket_server import WebSocketServer, WebSocket

class SimpleEcho(WebSocket):
    def handle(self):
        # echo message back to client
        self.send_message(self.data)

    def connected(self):
        print(self.address, 'connected')

    def handle_close(self):
        print(self.address, 'closed')

server = WebSocketServer('', 8000, SimpleEcho)
server.serve_forever()

相关问题