JavaScript WebSocket OnOpen not called

837
May 01, 2017, at 07:37 AM

I can't seem to get this WebSocket connection working. Here is my server:

const express = require('express');
const app = express();
const http = require('http').Server(app);
const WSServer = require('uws');
app.use(express.static('./client'));
var wss = new WSServer({ http });
const port = 4007;
wss.on('connection', (socket)=> {
  socket.send("Bonjour");
  socket.on('message', parser);
});
function parser(message) {
  message = JSON.parse(message);
  console.log(message[0]);
}
http.on("upgrade", () => { wss.handleUpgrade });
http.listen(port, () => {

On my server, the "connection" event is never triggered. Here is my client:

var socket = new WebSocket(window.location.origin.replace(/https?/g, "ws"));
var data = ["hello"];
socket.onopen = function() {
    socket.send(JSON.stringify(data));
};
socket.onmessage = function(message) {
    alert(message);
    return false;
};

The client never receives "Bonjour", the onopen function is never triggered, and if I attempt to send a message outside of the onopen function, I get this error:

"Uncaught DOMException: Failed to execute 'send' on 'WebSocket': Still in CONNECTING state."

Any help will be appreciated.

READ ALSO
MySQL result from node.js in html, by react.js

MySQL result from node.js in html, by react.js

I make my first web application with nodes and reaccI do sql query in node

363
Google Actions API webhook response on Heroku nodejs

Google Actions API webhook response on Heroku nodejs

Im trying to use a user's input to be included in the webhook post so that I can use it in my appMy app originally turns house components on or off

478
Passport.js Github Strategy on Node.js: Github redirect_uri mismatch

Passport.js Github Strategy on Node.js: Github redirect_uri mismatch

I want to implement a login with Passportjs and Github Strategy

478
Integrate Disqus in Hexo

Integrate Disqus in Hexo

I have a very basic but very annoyed question for me that I have a Hexo blog and want to integrate Disqus Comments capabilityI'm using the Beantech Hexo theme and it has a disqus_username: kjanshair property in _config

336