Open hypo-thesis opened 4 years ago
Your problem if that's really your error log is that you didn't change the wss://ws.finnhub.io/?token=mytoken to your own unique token. You get this token from first, subscribing to the site.
The button to get a free one is literally on the front page of finnhub.io AKA Get Free API key
Here is a working example for ya. Hope this helps. Just uncomment the streams you wanna subscribe to.
<!DOCTYPE html>
<html>
<head>
<title></title>
<style type="text/css">
body {
background-color: #121212;
}
</style>
</head>
<body>
<script type="text/javascript">
const socket = new WebSocket('wss://ws.finnhub.io?token=API_TOKEN');
console.info('1. New websocket created.');
// Connection opened -> Subscribe
socket.addEventListener('open', function (event) {
socket.send(JSON.stringify({'type':'subscribe', 'symbol': 'AAPL'}))
// socket.send(JSON.stringify({'type':'subscribe-news', 'symbol': 'AAPL'}))
// socket.send(JSON.stringify({'type':'subscribe', 'symbol': 'BINANCE:BTCUSDT'}))
// socket.send(JSON.stringify({'type':'subscribe', 'symbol': 'IC MARKETS:1'}))
console.info('2. Subscribing to symbols...');
});
// Listen for messages from the websocket stream...
socket.addEventListener('message', function (event) {
console.clear();
console.info('1. New websocket created.');
console.info('2. Subscribing to symbols...');
console.info('3. Websocket streaming.');
// stream response...
let response = JSON.parse(event.data);
// Determine response type.
switch(response.type) {
// Just a Websocket server ping.
case 'ping':
// ping.
console.warn('Occasional server', response.type + '.');
break;
case 'trade':
// type = trade. Obtain nodes.
console.warn(response.data[0].t, ':', response.data[0].s, '$' + response.data[0].p);
break;
default:
// anything else we haven't caught yet.
console.log(response);
}
});
// Unsubscribe
var unsubscribe = function(symbol) {
socket.send(JSON.stringify({'type':'unsubscribe','symbol': symbol}))
}
</script>
</body>
</html>
I wanted to mask my token obviously and mytoken
represents actually the api token. I get that error from time to time. Happens at least 5 hours every week. Is this a server issue?
@suchislife801 do you need to handle ping responses? or is it implemented in the ws implementation?
这个问题我几个月前就已经反馈过,但没有结果,付费与免费一样,经常在晚上停机,一停就几小时
I am getting
reconnecting-websocket-mjs.js:518 WebSocket connection to 'wss://ws.finnhub.io/?token=mytoken' failed: Error during WebSocket handshake: Unexpected response code: 200
on my codefor the past 2 hours and I wonder if there is a problem with my code or the server. How to avoid this?