【发布时间】:2017-08-21 17:04:46
【问题描述】:
背景
- 我正在将 Unity 应用程序连接到 SocketIO 应用程序
- Unity 正在使用 SocketIO 插件(仅使用 websockets,不使用 polling/xhr)
要从 Unity 连接到我的 websocket,我使用的是 ws://localhost:3000/socket.io/?EIO=4&transport=websocket。如果我通过浏览器点击该链接,我会看到以下内容:
{"code":3,"message":"Bad request"}
此时,我使用 io.set('transports', ['websocket']); 为我的 NodeJS 应用程序强制使用 websocket,但随后我的应用程序停止工作。
问题
如何确保 websocket 可用于 NodeJS+SocketIO?
代码
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']);
...
设置
- Ubuntu@14.04
- NodeJS@0.10.25
- SocketI@O1.7.3
- express@4.15.2
【问题讨论】:
-
你为什么使用 ws 和 socket.io ?似乎你应该只需要一个,如果你只想要 websockets 使用 ws ?
标签: node.js websocket socket.io