WebSocket connection to 'wss://url' failed: Unknown reason - websocket

the WebSocket connection is some time.
WebSocket connection to 'localhost:8002/WebDav/doc1.docx' failed: Unknown reason
not getting what is the Unknown reason. please advise any solution to this.
code -
var socket = new WebSocket("wss://url");
socket.addEventListener('message', function (e)
{
var notifyObject = JSON.parse(e.data); // Checking message type after receiving.
if(notifyObject.EventType === "refresh") { }
}, false)

Related

Receiving error message when trying to connect to GraphQL api over websocket using k6

I'm going to perform a subscription test using k6 for the graphql api that uses Hasura. Here's what I've tried:
import ws from "k6/ws";
import { check } from "k6";
export const myFunc = (url, access_token, id, query) => {
const headers = {
Authorization: access_token,
};
const res = ws.connect(url, { headers }, function (socket) {
socket.on("open", function open() {
console.log(`VU ${__VU}: connected`);
socket.send(
JSON.stringify({
type: "connection_init",
payload: headers,
})
);
console.log("sending query");
socket.send(
JSON.stringify({
type: "start",
payload: {
query: query,
variables: {
id,
}
},
})
);
});
...
socket.on("message", function (msg) {
console.log(`VU ${__VU}: received message: ${msg}`);
const message = JSON.parse(msg);
if (message.type == "connection_ack")
console.log("Connection Established with WebSocket");
if (message.type == "data") console.log(`Message Received: ${message}`);
});
...
};
And the logs with error:
INFO[0001] VU 1: connected source=console
INFO[0001] sending query source=console
INFO[0001] VU 1: received message: {"type":"ka"} source=console
INFO[0001] VU 1: received message: {"type":"connection_ack"} source=console
INFO[0001] Connection Established with WebSocket source=console
INFO[0001] VU 1: received message: {"type":"ka"} source=console
INFO[0001] VU 1: received message: {"type":"connection_error","payload":"parsing ClientMessage failed: Error in $: When parsing the record StartMsg of type Hasura.GraphQL.Transport.WebSocket.Protocol.StartMsg the key id was not present."} source=console
Why am I receiving the key id not present error? I have no idea what that means and couldn't find anything when I searched for it.
When you send a message over established websocket connection, the protocol dictates that you need to send id too. See this link.
Example payload:
{
"id": "1",
"type":"start",
"payload": {
"variables":{},
"query":"query MyQuery {\n test {\n id\n }\n}\n"
}
}
id can be any string decided by the client. When server send the response back, it will contain the same id. With parsing ClientMessage failed error message, Hasura is complaining that it cannot find the id.

Coinbase-pro for Node.js - Websocket connection breaking with error: read ECONNRESET

I'm currently stuck with an issue I'm getting with the coinbase-pro-node npm package (https://github.com/coinbase/coinbase-pro-node). I'm connecting to the matches channel and listening for messages there but the connection with the Websocket breaks after a few hours without telling me much. I can't trace back the problem and it doesn't happen on the same intervals. Sometimes it breaks just minutes after I run the script. Thanks for the help.
The code:
const CoinbasePro = require('coinbase-pro');
var coinbaseWs = '';
function connect() {
coinbaseWs = new CoinbasePro.WebsocketClient(
['BTC-USD'],
'wss://ws-feed.pro.coinbase.com',
{
key: 'xxxx',
secret: 'xxxx',
passphrase: 'xxxx',
},
{ channels: ['matches'] }
);
coinbaseWs.on('message', async data => {
console.log(data)
});
coinbaseWs.on('error', err => {
console.error("Connection with Coinbase websocket failed with error: " + err);
console.log("Error stack trace: " + err.stack);
});
coinbaseWs.on('close', () => {
console.error("Connection with Coinbase websocket closed!");
});
}
connect();
Error stack:
Error: read ECONNRESET
File "internal/stream_base_commons.js", line 167, in TLSWrap.onStreamRead
it does break from time to time for no apparent reason. All you can do is listen for the heartbeat messages and use those to decide whether to re-initiate a new websocket feed. I raised a similar query directly with the coinbase pro/gdax customer support.

reCAPTCHA timing out on verify

Having made no changes (but also not tested this in a couple months), my reCAPTCHA code is timing out when trying to verify the user response (both on localhost and on our DEV server). The IP is rotating in the error message (216.58.218.228:443, 172.217.1.4:443, others). I would normally think that this is a firewall issue - I can't telnet to the IP/Port combinations in the error messages - but I can pull up https://www.google.com/recaptcha/api/siteverify in a browser and get an error response back quickly. Any ideas on what might be going on?
Code I normally use:
// Prepare input.
var input = new NameValueCollection
{
{ "response", captcha },
{ "secret", _settings.SecretKey }
};
// Make call.
byte[] response;
using (WebClient client = new WebClient())
{
response = client.UploadValues(_settings.VerificationUrl, Http.Verbs.POST, input);
}
// Parse response.
var body = Encoding.UTF8.GetString(response);
var result = JsonConvert.DeserializeObject<VerificationResponse>(body);
return result.Success;
Attempt at verifying through GET (as recommended here):
using (WebClient client = new WebClient())
{
var response = client.DownloadString($"https://www.google.com/recaptcha/api/siteverify?secret={_settings.SecretKey}&response={captcha}");
}
Example full error message:
A connection attempt failed because the connected party did not properly respond after a period of time, or established connection failed because connected host has failed to respond 216.58.218.228:443

How to tell when a Stomp server disconnected from the Stomp.JS client

I am sending a stomp message over a Sock.JS client. When I disconnect the server I would like a warning message to show up on the client. To do this I have implemented a server side heartbeat
stompClient = Stomp.over(socket);
stompClient.heartbeat.outgoing = 20000;
stompClient.heartbeat.incoming = 20000;
stompClient.connect({}, function(frame) {
...
}
In the Chrome developer console I see the message
POST http://localhost:8080/hello/800/8n_btbxb/xhr_streaming net::ERR_CONNECTION_RESET sockjs-0.3.min.js:27
Whoops! Lost connection to undefined
How can I capture this error message?
As pointed out by muttonUp stomp.js from https://github.com/jmesnil/stomp-websocket/ will overwrite the onclose handler. On the other hand it provides the option to pass an error-callback on connect:
stompClient.connect({}, function(frame) {
...
}, function(message) {
// check message for disconnect
});
Since you will get several kinds of errors delivered to your callback, you have to check the message it it was indeed the "Whoops! [...]" which indicates a connection loss.
Oops just figured it out I will delete in a bit if it doesn't help others. I needed to use SockJS client instead of the Stomp one...
var socket = new SockJS('/hello');
...
socket.onclose = function() {
console.log('close');
stompClient.disconnect();
setConnected();
};

Websocket messages mysteriously disappear

I'm going crazy trying to figure out what's wrong with my system that it is unable to send websocket messages. I've tried a ton of things:
Chrome 32, firefox 27, ie 10
various websocket server libraries: ws, websocket, nodejs-websocket
running the server on windows and centos
reinstalling node.js (to version 0.10.26)
reinstalling firefox
Turning off my firewall
The behavior is always the same: both client and server say they get a connection, but when the client tries to send a message, the other one simply doesn't get it. Sometimes i get "Error: read ECONNRESET" on the server - but not all the time. About 50% of the time it just fails silently. Eventually the connection closes.
If I add code to send a message from the server on connection, it always gets "Error: read ECONNRESET".
What does work?
the exact same code and setup on my laptop
any websocket code online, for example this: http://www.websocket.org/echo.html
So what could cause just my local websocket connection to silently drop messages?
Server:
var
ws = require('websocket').server
var http = require('http')
var server = http.createServer(function(request, response) {
console.log("connection received for "+request.url)
response.writeHead(404)
response.end()
})
server.listen(8080, function() {
console.log("now listening")
})
wsServer = new ws({httpServer:server, autoAcceptConnections: false})
wsServer.on('request', function(request) {
console.log("got request")
var connection = request.accept('a', request.origin)
console.log("got connection")
connection.on('message', function(message) {
console.log("got message: "+message)
})
connection.on("close", function() {
console.log("closed")
})
connection.on('error', function(e) {
console.log('Error: '+e)
})
})
Output:
got connection
closed
Client:
<script>
var ws = new WebSocket('ws://localhost:8080/', 'a')
ws.onopen = function() {
console.log('open')
ws.send('something2')
}
ws.onerror = function(e) {
console.log(e)
}
ws.onmessage = function(m) {
console.log(m)
}
ws.onclose = function() {
console.log('closed')
}
</script>
Output
open
closed
This is driving me crazy.

Resources