[ 
https://issues.apache.org/jira/browse/ARTEMIS-4961?page=com.atlassian.jira.plugin.system.issuetabpanels:comment-tabpanel&focusedCommentId=17869684#comment-17869684
 ] 

Justin Bertram edited comment on ARTEMIS-4961 at 7/30/24 3:56 PM:
------------------------------------------------------------------

I pulled out the JavaScript from your HTML and tried to execute it with 
{{node}}. However, even after changing the {{connectUrl}} to connect to a local 
instance of ActiveMQ Artemis the code still didn't run due to multiple syntax 
errors. I fixed those to produce this code in a file named {{mqtt.js}}:
{code:javascript}
const mqtt = require("mqtt");

const clientId = 'mqttjs_' + Math.random().toString(16).substring(2, 8)  
const connectUrl = 'ws://localhost:1883'
const options = {       
keepalive: 30,      
     clientId: clientId,       
     clean: true,       
     connectTimeout: 5000,       
     username: 'test',      
     password: 'test',       
     reconnectPeriod: 1000
}     
const topic = '/WebSocket/mqtt'     
const payload = 'WebSocket mqtt test'    
const qos = 0     
console.log('connecting mqtt client')     
const client = mqtt.connect(connectUrl, options)    
client.on('connect', () => {
    console.log('Client connected:' + clientId)
    client.subscribe(topic, { qos }, (error) => {         
        if (error) {           
           console.log('Subscribe error:', error)           
           return        
        }         
        console.log(`Subscribe to topic ${topic}`)      
    })
}){code}
This code runs fine in my environment. Here's what it logs:
{noformat}
$ node mqtt.js 
connecting mqtt client
Client connected:mqttjs_896ee4
Subscribe to topic /WebSocket/mqtt{noformat}
Your problem appears to be related to your environment. The "Connection reset 
by peer" issues indicate that something is closing the network connection 
between the client and the broker. The broker can't do anything about this. I 
recommend you inspect your environment for clues about whatever is causing this 
connection problem.


was (Author: jbertram):
I pulled out the JavaScript from your HTML and tried to execute it with 
{{node}}. However, even after changing the {{connectUrl}} to connect to a local 
instance of ActiveMQ Artemis the code still didn't run due to multiple syntax 
errors. I fixed those to produce this code:
{code:javascript}
const mqtt = require("mqtt");

const clientId = 'mqttjs_' + Math.random().toString(16).substring(2, 8)  
const connectUrl = 'ws://localhost:1883'
const options = {       
keepalive: 30,      
     clientId: clientId,       
     clean: true,       
     connectTimeout: 5000,       
     username: 'test',      
     password: 'test',       
     reconnectPeriod: 1000
}     
const topic = '/WebSocket/mqtt'     
const payload = 'WebSocket mqtt test'    
const qos = 0     
console.log('connecting mqtt client')     
const client = mqtt.connect(connectUrl, options)    
client.on('connect', () => {
    console.log('Client connected:' + clientId)
    client.subscribe(topic, { qos }, (error) => {         
        if (error) {           
           console.log('Subscribe error:', error)           
           return        
        }         
        console.log(`Subscribe to topic ${topic}`)      
    })
}){code}
This code runs fine in my environment. Here's what it logs:
{noformat}
$ node mqtt.js 
connecting mqtt client
Client connected:mqttjs_896ee4
Subscribe to topic /WebSocket/mqtt{noformat}
Your problem appears to be related to your environment. The "Connection reset 
by peer" issues indicate that something is closing the network connection 
between the client and the broker. The broker can't do anything about this. I 
recommend you inspect your environment for clues about whatever is causing this 
connection problem.

> How to solve mqtt connection over websocket when  visiting   from  Wide Area 
> Network IP ?
> -----------------------------------------------------------------------------------------
>
>                 Key: ARTEMIS-4961
>                 URL: https://issues.apache.org/jira/browse/ARTEMIS-4961
>             Project: ActiveMQ Artemis
>          Issue Type: Bug
>          Components: Broker, MQTT
>    Affects Versions: 2.17.0, 2.24.0
>            Reporter: windy
>            Assignee: Justin Bertram
>            Priority: Major
>         Attachments: 小Q截图-20240729152045.png
>
>
> !小Q截图-20240729152045.png|width=494,height=210!
> {{broker.xml}}:
> {code:xml}
> <acceptor 
> name="wss">tcp://0.0.0.0:1883?tcpSendBufferSize=1048576;tcpReceiveBufferSize=1048576;protocols=MQTT;useEpoll=true</acceptor>{code}
> {{testPage.html}}:
> {noformat}
> <!DOCTYPE html>
> <html lang="en">  
>    <head>
>       <meta charset="UTF-8" />
>       <meta name="viewport" content="width=device-width, initial-scale=1.0" />
>       <title>MQTT over WebSocket</title>
>       <script src="https://unpkg.com/mqtt/dist/mqtt.min.js";></script>   
>    </head>   
>    <body>   
>    </body>  
>    <script>        
>       const clientId = 'mqttjs_' + Math.random().toString(16).substring(2, 8) 
>  
>       const connectUrl = 'ws://218.106.xxxxx:1883'     //Wide Area Network IP
>       const options = {       
>          keepalive: 30,      
>          clientId: clientId,       
>          clean: true,       
>          connectTimeout: 5000,       
>          username: 'test',      
>          password: 'test',       
>          reconnectPeriod: 1000    
>    
>       }     
>       const topic = '/WebSocket/mqtt'     
>       const payload = 'WebSocket mqtt test'    
>       const qos = 0     
>       console.log('connecting mqtt client')     
>       const client = mqtt.connect(connectUrl, options)    
>       client.on('connect', () => {
>          console.log('Client connected:' + clientId)
>          client.subscribe(topic, { qos }, (error) => {         
>             if (error) {           
>                console.log('Subscribe error:', error)           
>                return        
>             }         
>             console.log(`Subscribe to topic ${topic}`)      
>          }
>       ) 
>    </script>
> </html>{noformat}
>  
> {{artemis.log}}:
> {noformat}
> 2024-07-30 13:34:25,017 WARN  [DefaultChannelPipeline:1152] An 
> exceptionCaught() event was fired, and it reached at the tail of the 
> pipeline. It usually means the last handler in the pipeline did not handle 
> the exception.: io.netty.channel.unix.Errors$NativeIoException: 
> readAddress(..) failed: Connection reset by peer
> 2024-07-30 13:40:12,587 WARN  [DefaultChannelPipeline:1152] An 
> exceptionCaught() event was fired, and it reached at the tail of the 
> pipeline. It usually means the last handler in the pipeline did not handle 
> the exception.: io.netty.channel.unix.Errors$NativeIoException: 
> readAddress(..) failed: Connection reset by peer
> 2024-07-30 13:55:59,399 WARN  [DefaultChannelPipeline:1152] An 
> exceptionCaught() event was fired, and it reached at the tail of the 
> pipeline. It usually means the last handler in the pipeline did not handle 
> the exception.: io.netty.channel.unix.Errors$NativeIoException: 
> readAddress(..) failed: Connection reset by peer{noformat}



--
This message was sent by Atlassian Jira
(v8.20.10#820010)

---------------------------------------------------------------------
To unsubscribe, e-mail: issues-unsubscr...@activemq.apache.org
For additional commands, e-mail: issues-h...@activemq.apache.org
For further information, visit: https://activemq.apache.org/contact


Reply via email to