r/websocket • u/GasOutrageous9972 • Feb 01 '23
WebSocket Server Help Needed
Hi Everyone!
Some of the posts on here are really great!
Im a noob here and in the field of what I'm trying to do too. I have started to create a multiplayer game but it was using short polling and I don't think that is going to be best long term.
After completing a basic POC I started to rebuild and decided to use websockets as a solution.
I'm running a virtual private Linux server from Ionos Plesk is installed I have created a subdirectory called server in httpdocs which is where the app.js file is for server creation. Port 8080 is open Im using http/ws for now before I attempt to go near https/wss I have enabled NodeJS on the server and set up the following as config:
Document Root: /httpdocs/server
Application URL: http://[my.url]
Application Root /httpdocs/server
Application Startup File app.js
The app.js contains:
var Msg = ''; var WebSocketServer = require('ws').Server , wss = new WebSocketServer({port: 8080}); wss.on('connection', function(ws) { ws.on('message', function(message) { console.log('Received from client: %s', message); ws.send('Server received from client: ' + message); }); }); On any web page that loads on that domain I now get an error stating 'Upgrade Required' error 426 I know that means the server wants the client to upgrade to websockets but I cant understand why the client isn't asking to upgrade back.. In my ws_test.html file I access to test the connection this is the code in the HTML body:
<script> let socket = new WebSocket("ws://[my.url]/server/app.js"); socket.onopen = function(e) { alert("[open] Connection established"); alert("Sending to server"); socket.send("TEST SUCCESSFUL"); }; </script> I have tried so many different things and come to the end of my tether.. I have changed the connection URL many times and the ports. I suspect NGINX may have something to do with it but I tried disabling proxy and no change.
Can anyone see any glaringly obvoius mistakes!?
Thanks SO SO much for just reading this any help will be hugely appreciated.
I have tried so many different things and come to the end of my tether.. I have changed the connection URL many times and the ports. I suspect NGINX may have something to do with it but I tried disabling proxy and no change.
Can anyone see any glaringly obvoius mistakes!?
Thanks SO SO much for just reading this any help will be hugely appreciated.
View all comments
Show parent comment
1
u/sylyac2000 Jun 11 '23
why not using socket.io? there are lot of sample with socket.io