繁体   English   中英

使用套接字io而不返回index.html会引发错误

[英]Using socket io without returning the index.html throws an error

我想设置websocket,而不必返回index.html文件

我仍然是套接字io的新手,这是我尝试过的

通过安装IO

npm install socket.io --save

创建index.js

var http = require('http');
var fs = require('fs');


  // Loading socket.io
 var io = require('socket.io');

  // When a client connects, we note it in the console
  io.sockets.on('connection', function (socket) {
   console.log('A client is connected!');
 });


 server.listen(1100);

现在当我运行节点索引时出现错误

   io.sockets.on('connection', function (socket) {
      ^

  TypeError: Cannot read property 'on' of undefined

我想做的是将websocet连接到我的vuejs客户端,因为我不想显示html但要使用套接字事件,因此,我还是跳过了显示html的部分。 我要去哪里错了?

嘿,您需要将socket.io附加到http服务器,以使代码正常工作并侦听传入的事件。

var express = require('express');
var app = express();
var server = require('http').Server(app);
var io = require('socket.io').listen(server);

//io.on is the shorter form of io.sockets.on
io.on('connection', function(socket){
  console.log('user connected');
});

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM