【发布时间】:2020-07-14 19:00:58
【问题描述】:
在我非常简单的 nodejs 服务器应用程序中。我提供 socket.io (v2.2.0) 没有 express 或 http。这是我的代码。
const io = require('socket.io')();
io.on('connection', (client) => {
client.emit("welcome", "hello user");
});
io.listen(3000);
(通过谷歌搜索)我发现我可以在我的网络浏览器中导航到两者 http://localhost:3000/socket.io/ 和 http://localhost:3000/socket.io/socket.io.js 这些“端点”是从哪里定义的?我的代码没有说明这些“端点”,但它们确实存在。这些是过去版本的遗留物吗?
当我访问 http://localhost:3000/socket.io/ 时,我收到一个 400 错误请求,其中包含 JSON 返回消息
{"code":0,"message":"Transport unknown"}
当我访问 http://localhost:3000/socket.io/socket.io.js 我得到了这个
!function(t,e){"object"==typeof exports&&"object"==typeof module?module.exports=e():"function"==typeof define&&define.amd?define([],e):"object"==typeof exports?exports.io=e():t.io=e()}(this,function(){return function(t){function e(n){if(r[n])return r[n].exports;var o=r[n]={exports:{},id:n,loaded:!1};return t[n].call(o.exports,o,o.exports,e),o.loaded=!0,o.exports}var r={};return e.m=t,e.c=r,e.p="",e(0)}([function(t,e,r){"use strict";function n(t,e){"object"===("undefined"==typeof t?"undefined":i(t))&&(e=t,t=void 0),e=e||{};var r,n=s(t),a=n.source,p=n.id,f=n.path,l=h[p]&&f in h[p].nsps,d=e.forceNew||e["force new connection"]||!1===e.multiplex||l;return d?(u("ignoring socket cache for %s",a),r=c(a,e)):(h[p]||(u("new io instance for %s",a),h[p]=c(a,e)),r=h[p]),n.query&&!e.query?e.query=n.query:e&&"object"===i(e.query)&&(e.query=o(e.query)),r.socket(n.path,e)}function o(t){var e=[];for(var r in t)t.hasOwnProperty(r)&&e.push(encodeURIComponent(r)+"="+encodeURIComponent(t[r]));return e.join("&")}var i="function"==typeof Symbol&&"symbol"==typeof Symbol.iterator?function(t){return typeof t}:function(t){return t&&"function"==typeof
and so on
【问题讨论】: