dotnet / MQTTnet

MQTTnet is a high performance .NET library for MQTT based communication. It provides a MQTT client and a MQTT server (broker). The implementation is based on the documentation from http://mqtt.org/.
MIT License
4.51k stars 1.07k forks source link

mqttnet.server and paho.js #918

Closed danielstock closed 4 years ago

danielstock commented 4 years ago

Describe your question

i wrote a console application with mqttnet.server. mqtt.fx client connected to it successfully.

then i tried to use java script in web browser (paho-mqtt/1.0.1/mqttws31.min.js) the code is quite simple: var cid= (Math.random() * 1000000000).toString(); var client = new Paho.MQTT.Client('127.0.0.1', Number(8222), cid); client.onConnectionLost = onConnectionLost; ... but i got the following response: mqttws31.min.js:36 WebSocket connection to 'ws://127.0.0.1:8222/mqtt' failed: Error in connection establishment: net::ERR_CONNECTION_REFUSED

so is there anyway i could do this using mqttnet.server (i expect the server to run in command line), other than using mqttnet.asp.net.core ?

thanks a lot

Which project is your question related to?

chkr1011 commented 4 years ago

Hi, thank you for reporting the issue. This is indeed a bug in the standalone MQTTnet.Server. This will be fixed in the next version (3.0.11). Best regards Christian

danielstock commented 4 years ago

@chkr1011 thanks.

danielstock commented 4 years ago

@chkr1011 i saw the uploaded files of js examples. but it's still not working,

i updated mqttnet nuget package from 3.09 to 3.0.11, and my program code is as following: var optionsBuilder = new MqttServerOptionsBuilder() .WithConnectionBacklog(100) .WithDefaultEndpointPort(1883) .WithConnectionValidator(c => {
c.ReasonCode = MqttConnectReasonCode.Success; }).WithSubscriptionInterceptor(c => { // if (context.TopicFilter.Topic == "x") c.AcceptSubscription = true; }).WithApplicationMessageInterceptor(c => { //c.ApplicationMessage.Payload = new byte[]... c.AcceptPublish = true; });

// create mqtt server. _mqttServer = new MqttFactory().CreateMqttServer(); await _mqttServer.StartAsync(optionsBuilder.Build());

is there anything i'm missing?

thanks.

chkr1011 commented 4 years ago

Well you need a Asp.net project. A plain console application with mqttnet only will not work. Check out the mqttnet server project for more details or checkout the wiki. There are also some examples.

danielstock commented 4 years ago

@chkr1011 thanks. you mean integration with asp.net.core ?