node.js - NodesJS SocketIO - How can I make sure websockets are being used? -


background

  • i'm working connect unity application socketio application
  • unity using socketio plugin (only uses websockets, not polling/xhr)

to connect websocket unity i'm using ws://localhost:3000/socket.io/?eio=4&transport=websocket. if hit link via browser see following:

{"code":3,"message":"bad request"} 

at point forced websockets nodejs application io.set('transports', ['websocket']); application stopped working.

question

how can make sure websockets available nodejs+socketio?

code

app.js

var app = require('express')(); var http = require('http').server(app);  app.get('/', function(req, res){         res.sendfile(__dirname + '/index.html'); });  var server = app.listen(3000, function () {    var host = server.address().address    var port = server.address().port }); var io = require('socket.io').listen(server); //io.set('transports', ['websocket']);  ... 

setup

  • ubuntu@14.04
  • nodejs@0.10.25
  • socketi@o1.7.3
  • express@4.15.2

you seem using both websocket , socket.io , can cause conflicts. should choose 1 , stick it. if you're sure client use websocket there no need use socket.io @ all. use websocket ad have worry ajax, comet, long-polling workarounds.

this example server-side code using websocket:

var path = require('path'); var app = require('express')(); var ws = require('express-ws')(app); app.get('/', (req, res) => {   console.error('express connection');   res.sendfile(path.join(__dirname, 'ws.html')); }); app.ws('/', (s, req) => {   console.error('websocket connection');   (var t = 0; t < 3; t++)     settimeout(() => s.send('message server', ()=>{}), 1000*t); }); app.listen(3001, () => console.error('listening on http://localhost:3001/')); console.error('websocket example'); 

as can see pretty simple.

see code examples in answers more info:


Comments

Popular posts from this blog

javascript - Clear button on addentry page doesn't work -

c# - Selenium Authentication Popup preventing driver close or quit -

tensorflow when input_data MNIST_data , zlib.error: Error -3 while decompressing: invalid block type -