项目要导出到html,现在不支持c#导出只能用GDScript!!!!!
网上找到了使用普通socket连接SignalR的解决方案,地址:https://trailheadtechnology.com/using-postman-with-signalr-websockets-development/
按照方案执行:
1、正常连接到服务器WebSocket
2、发送 {"protocol": "json","version": 1}0x1e
3、发送 {"arguments":["2"],"invocationId":"0","target":"Subscribe","type":1}0x1e 获取订阅数据
# arguments[] 中传的是参数 target是方法 invocationId 保存默认 type保存默认
演示:
按照这个方法能正常获取数据了,但在godot中,使用WebSocketPeer连接后发送步骤一返回正常,发送数据二就断开连接,没有报错信息,来个大佬帮我看看是不是封装的客服端问题。
WebSocket 代码:
extends Node
class_name WebSocketClient
var socket=WebSocketPeer.new();
var last_state=WebSocketPeer.STATE_CLOSED;
signal connected_to_server();
signal connection_closed();
signal message_received(message:Variant);
func poll()->void:
if socket.get_ready_state()!=socket.STATE_CLOSED:
socket.poll()
var state=socket.get_ready_state()
if last_state!=state:
last_state=state
if state==socket.STATE_OPEN:
connected_to_server.emit()
elif state==socket.STATE_CLOSED:
connection_closed.emit()
while socket.get_ready_state()==socket.STATE_OPEN and socket.get_available_packet_count():
message_received.emit(get_message())
func get_message()->Variant:
if socket.get_available_packet_count()<1:
return null;
var packet=socket.get_packet();
if socket.was_string_packet():
return packet.get_string_from_utf8()
return bytes_to_var(packet)
func send(message)->int:
if typeof(message)==TYPE_STRING:
return socket.send_text(message);
return socket.send(var_to_bytes(message))
func connect_to_url(url)->int:
var error=socket.connect_to_url(url);
if error!=OK:
return error;
last_state=socket.get_ready_state()
return OK;
func close(code:=1000,reason:="")->void:
socket.close(code,reason);
last_state=socket.get_ready_state()
func get_socket()->WebSocketPeer:
return socket;
func _process(delta):
poll()
网上找到了使用普通socket连接SignalR的解决方案,地址:https://trailheadtechnology.com/using-postman-with-signalr-websockets-development/
按照方案执行:
1、正常连接到服务器WebSocket
2、发送 {"protocol": "json","version": 1}0x1e
3、发送 {"arguments":["2"],"invocationId":"0","target":"Subscribe","type":1}0x1e 获取订阅数据
# arguments[] 中传的是参数 target是方法 invocationId 保存默认 type保存默认
演示:
按照这个方法能正常获取数据了,但在godot中,使用WebSocketPeer连接后发送步骤一返回正常,发送数据二就断开连接,没有报错信息,来个大佬帮我看看是不是封装的客服端问题。
WebSocket 代码:
extends Node
class_name WebSocketClient
var socket=WebSocketPeer.new();
var last_state=WebSocketPeer.STATE_CLOSED;
signal connected_to_server();
signal connection_closed();
signal message_received(message:Variant);
func poll()->void:
if socket.get_ready_state()!=socket.STATE_CLOSED:
socket.poll()
var state=socket.get_ready_state()
if last_state!=state:
last_state=state
if state==socket.STATE_OPEN:
connected_to_server.emit()
elif state==socket.STATE_CLOSED:
connection_closed.emit()
while socket.get_ready_state()==socket.STATE_OPEN and socket.get_available_packet_count():
message_received.emit(get_message())
func get_message()->Variant:
if socket.get_available_packet_count()<1:
return null;
var packet=socket.get_packet();
if socket.was_string_packet():
return packet.get_string_from_utf8()
return bytes_to_var(packet)
func send(message)->int:
if typeof(message)==TYPE_STRING:
return socket.send_text(message);
return socket.send(var_to_bytes(message))
func connect_to_url(url)->int:
var error=socket.connect_to_url(url);
if error!=OK:
return error;
last_state=socket.get_ready_state()
return OK;
func close(code:=1000,reason:="")->void:
socket.close(code,reason);
last_state=socket.get_ready_state()
func get_socket()->WebSocketPeer:
return socket;
func _process(delta):
poll()