admin管理员组文章数量:1191748
Looking at the following example from the Socket.IO lib (or any other example):
// note, io(<port>) will create a http server for you
var io = require('socket.io')(80);
io.on('connection', function (socket) {
io.emit('this', { will: 'be received by everyone'});
socket.on('private message', function (from, msg) {
console.log('I received a private message by ', from, ' saying ', msg);
});
socket.on('disconnect', function () {
io.emit('user disconnected');
});
});
It seems that Socket.IO is always dependent on a http server, to the point that it will create one for you, like in the example above.
Since websockets are not HTTP, why are http servers needed? If it is only for fallbacks, why is it so thoughly integrated?
Looking at the following example from the Socket.IO lib (or any other example):
// note, io(<port>) will create a http server for you
var io = require('socket.io')(80);
io.on('connection', function (socket) {
io.emit('this', { will: 'be received by everyone'});
socket.on('private message', function (from, msg) {
console.log('I received a private message by ', from, ' saying ', msg);
});
socket.on('disconnect', function () {
io.emit('user disconnected');
});
});
It seems that Socket.IO is always dependent on a http server, to the point that it will create one for you, like in the example above.
Since websockets are not HTTP, why are http servers needed? If it is only for fallbacks, why is it so thoughly integrated?
Share Improve this question asked Jun 15, 2016 at 12:56 omertsomerts 8,8382 gold badges33 silver badges40 bronze badges 8 | Show 3 more comments1 Answer
Reset to default 29Since websockets are not HTTP, why are http servers needed?
The premise on which your question seems to be based is that socket.io
is a websocket library, which it isn't.
It's a real time, bidirectional event-based communication library (blurb from homepage). One of the transports that it uses are websockets, but it also provides other transports (XHR/JSONP), not just as a fallback but also for situations where websockets aren't supported/required/wanted.
On top of the transport, it offers additional functionality like segmentation (namespaces, rooms), acknowledgements, broadcasts, etc.
Even when websockets can be used, the initial connection setup it done over HTTP. Also, a socket.io
server will attach to an HTTP server so it can serve its own client code through /socket.io/socket.io.js
.
That said, although you don't need an HTTP server to regular websockets, there's no denying that the websocket protocol was designed with HTTP in mind (as to allow HTTP and websocket servers to co-exist on the same TCP port).
本文标签: javascriptsocketiowhy does it need an http serverStack Overflow
版权声明:本文标题:javascript - Socket.io, why does it need an http server? - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1738457131a2087819.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
socket.io
isn't a pure Websocket server/implementation, it depends on HTTP for its initial connection setup. – robertklep Commented Jun 15, 2016 at 13:10ws
package is for you. It creates a plainWebSocket
server, without a fully-functional HTTP server. – Константин Ван Commented Nov 24, 2022 at 14:30