哪里包含socket.io-client和socket.io-stream客户端?

时间:2017-03-17 00:23:56

标签: socket.io

我是javascript的新手,我在这里关注示例:https://www.npmjs.com/package/socket.io-stream尝试设置一个非常简单的文件传输,只要客户端连接就自动传输文件。

但是,我收到错误:

Error: Module name "socket.io-client" has not been loaded yet for context

当我删除该行只是为了查看流是否正常工作时,我得到:

Error: Module name "socket.io-stream" has not been loaded yet for context

socket.io-client缺少对我有意义(因为我没有将它包含在我的<script>标签中的任何位置)。但是,上面列出的教程没有提到这一点。当我在教程中遵循他们的建议时,socket.io-stream缺失对我没有意义

cp node_modules/socket.io-stream/socket.io-stream.js public/static/js

(index.html驻留在public /中)并将JS文件包含在我的index.html

很难在这个问题上找到答案,非常感谢您提供的任何帮助!

这是我的服务器端代码:

var io = require('socket.io').listen(80); 
var ss = require('socket.io-stream');
var path = require('path');

io.of('/').on('connection', function(socket) {
  ss(socket).on('write-file', function(stream, data) {
    var filename = path.basename(data.name);
    stream.pipe(fs.createWriteStream(filename));
  });
});

和客户端代码:

的index.html:

<script src="/socket.io/socket.io.js"></script>
<script src="/static/js/socket.io-stream.js"></script>
<script src="/static/js/require.js"></script>
<script
   src="https://code.jquery.com/jquery-3.1.1.min.js"
   integrity="sha256-hVVnYaiADRTO2PzUGmuLJr8BLUSjGIZsDYGmIJLv2b8="
   crossorigin="anonymous"></script>
<script src="/client.js"></script>

client.js:

var io = require('socket.io-client');
var ss = require('socket.io-stream');

var socket = io.connect('http://localhost:3000');
var stream = ss.createStream();

ss(socket).emit('write-file', stream, { name: 'test.txt' });
fs.createReadStream('test.txt').pipe(stream);

这是我项目的树目录

├── index.js
├── node_modules
│   ├── socket.io
│   ├── socket.io-stream
│   ├── socket.io-client
│   ├──  ... a billion other node modules
└── public
    ├── index.html
    ├── static
    │   ├── glyphicons
    │   ├── js
    │   │   ├── client.js
    │   │   ├── require.js
    │   │   └── socket.io-stream.js
    │   └── style.css
    ├── test.txt

0 个答案:

没有答案