dod*_*ale 7 node.js socket.io ecmascript-6
对于有些琐碎的问题,我感到非常沮丧。
我正在用ES6编写并使用webpack编译的node.js服务器上运行一个快速应用程序。除以下警告外,它编译时没有任何错误:
../node_modules/socket.io/lib/index.js 113:11-32
0:0 warning Critical dependency: the request of a dependency is an expression
Run Code Online (Sandbox Code Playgroud)
(尽管我不确定这是否与当前问题有关),
但是,当我启动服务器时,出现以下错误:
Error: Cannot find module 'socket.io-client/dist/socket.io.js'
at Function.webpackEmptyContext [as resolve] (webpack:///../node_modules/socket.io/lib_sync?:2:10)
at resolvePath (webpack:///../node_modules/socket.io/lib/index.js?:113:100)
at Server.serveClient (webpack:///../node_modules/socket.io/lib/index.js?:116:25)
at new Server (webpack:///../node_modules/socket.io/lib/index.js?:53:8)
at Function.Server [as listen] (webpack:///../node_modules/socket.io/lib/index.js?:44:41)
at new Socket (webpack:///./server/socket.js?:10:98)
Run Code Online (Sandbox Code Playgroud)
套接字类:
import io from 'socket.io';
export default class Socket {
constructor(server) {
this.io = io.listen(server);
this.io.on('connection', (socket) => {
// Handle connection
});
}
}
Run Code Online (Sandbox Code Playgroud)
服务器参数是从express.listen()函数返回的对象。
我使用命令安装了socket.io npm install socket.io --save,随后"socket.io": "^2.1.1"在我的package.json中添加了依赖项,并运行命令npm install来更新我的依赖项,但仍然遇到错误。
正如你所知道的,我对此很迷惑......特别是几乎完全遵照docs后,这里因此任何帮助彻底感谢!
这就是我的诀窍:
var io = require('socket.io')(server, { serveClient: false })
Run Code Online (Sandbox Code Playgroud)
包socket.io取决于socket.io-client(因此它应该可用)。虽然文档可能已经解释了问题(很难说,因为没有相关代码)。
\n\n\n从 3.0 开始,快速应用程序已成为您传递给
\nhttp或http Server实例的请求处理程序函数。您需要传递Servertosocket.io,而不是 Express 应用程序函数。还要确保调用.listen的是server,而不是app。
var app = require(\'express\')();\nvar server = require(\'http\').createServer(app);\nvar io = require(\'socket.io\')(server);\nio.on(\'connection\', function(){ /* \xe2\x80\xa6 */ });\nserver.listen(3000);\nRun Code Online (Sandbox Code Playgroud)\n
| 归档时间: |
|
| 查看次数: |
1242 次 |
| 最近记录: |