错误描述
错误日志
OnError reason:
'Request Finished with Error! Exception: Network error!
TCP Connection got closed before receiving any data!
at BestHTTP.HTTPResponse.Receive
OnDisconnected - code: UnspecifiedError,
reason: 'Request Finished with Error! Exception: Network error!
TCP Connection got closed before receiving any data!
Unity的运行状态下连接MQTT服务器,是用的是WebSocket连接就会提示,下面是我的连接代码(错误的)。
ConnectionOptionsBuilder connectionOptionsBuilder;
connectionOptionsBuilder = new ConnectionOptionsBuilder()
.WithWebSocket(hostName, port)
.WithProtocolVersion(SupportedProtocolVersions.MQTT_3_1_1);
client = new MQTTClientBuilder().WithOptions(connectionOptionsBuilder)
.WithEventHandler(OnConnected)
.WithEventHandler(OnDisconnected)
.WithEventHandler(OnStateChanged)
.WithEventHandler(OnError)
.CreateClient();
解决方式
在编辑器下是用TCP连接
ConnectionOptionsBuilder connectionOptionsBuilder;
connectionOptionsBuilder = new ConnectionOptionsBuilder()
.WithTCP(hostName, port)
.WithProtocolVersion(SupportedProtocolVersions.MQTT_3_1_1);
client = new MQTTClientBuilder().WithOptions(connectionOptionsBuilder)
.WithEventHandler(OnConnected)
.WithEventHandler(OnDisconnected)
.WithEventHandler(OnStateChanged)
.WithEventHandler(OnError)
.CreateClient();
在WebGL下是用WebSocket连接
ConnectionOptionsBuilder connectionOptionsBuilder;
connectionOptionsBuilder = new ConnectionOptionsBuilder().
WithWebSocket(hostName,port).
WithTLS()
.WithProtocolVersion(SupportedProtocolVersions.MQTT_3_1_1);
client = new MQTTClientBuilder().WithOptions(connectionOptionsBuilder)
.WithEventHandler(OnConnected)
.WithEventHandler(OnDisconnected)
.WithEventHandler(OnStateChanged)
.WithEventHandler(OnError)
.CreateClient();