Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/sockets/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Node.js Socket.IO和Electron can';不发射或接收_Node.js_Sockets_Socket.io_Electron_Vue Cli - Fatal编程技术网

Node.js Socket.IO和Electron can';不发射或接收

Node.js Socket.IO和Electron can';不发射或接收,node.js,sockets,socket.io,electron,vue-cli,Node.js,Sockets,Socket.io,Electron,Vue Cli,我有一个用VueCLI启动的Electron项目和一个litle nodejssocket.io服务器,下面是服务器的文件: const http = require('http'); const express = require('express'); const socketio = require('socket.io'); const { userJoin, getCurrentUser, userLeave, getRoomUsers, users } = req

我有一个用
VueCLI
启动的
Electron
项目和一个litle nodejs
socket.io
服务器,下面是服务器的文件:

const http = require('http');
const express = require('express');
const socketio = require('socket.io');
const {
  userJoin,
  getCurrentUser,
  userLeave,
  getRoomUsers,
  users
} = require('./utils/users');

const app = express();
const server = http.createServer(app);
const io = socketio(server);

// Set static folder
app.use(express.static(path.join(__dirname, 'public')));

// Run when client connects
io.on('connection', socket => {

  console.log(`Connected tp ${socket.id}`)
  app.get('/send-file', (req, res, next) => {
      res.send('Sent')
  })

  socket.on('joinRoom', (args)=>{
    console.log('joinroom')
  })


  // Runs when client disconnects
  socket.on('disconnect', () => {
    const user = userLeave(socket.id);
  });
});



const PORT = process.env.PORT || 7575;

server.listen(PORT, () => console.log(`Server running on port ${PORT}`));

这是我的
preload.js
文件:

const io = require('socket.io-client');
window.socket = io('http://localhost:7575');

window.socket.on('welcome', () => {
    console.log('on welcome : welcome received renderer'); // displayed
    window.socket.emit('test')
});
window.socket.on('error', (e) => {
  console.log(e); // displayed ?
});
window.socket.on('ok', () => {
    console.log("OK received renderer"); // displayed
});
window.socket.on('connect', () => {
    console.log("connected renderer"); // displayed
    window.socket.emit('test');
});
下面是我的
createWindow
函数:

async function createWindow() {
  // Create the browser window.
  win = new BrowserWindow({
    width: 700,
    height: 600,
    webPreferences: {
      // Use pluginOptions.nodeIntegration, leave this alone
      // See nklayman.github.io/vue-cli-plugin-electron-builder/guide/security.html#node-integration for more info
      nodeIntegration: process.env.ELECTRON_NODE_INTEGRATION,
      enableRemoteModule: true,
      preload: path.join(__dirname, 'preload.js')
    }
  })
  win.setMenu(null)
  if (process.env.WEBPACK_DEV_SERVER_URL) {
    // Load the url of the dev server if in development mode
    await win.loadURL(process.env.WEBPACK_DEV_SERVER_URL)
    if (!process.env.IS_TEST) win.webContents.openDevTools()
  } else {
    createProtocol('app')
    // Load the index.html when not in development
    win.loadURL('app://./index.html')
  }
}
客户端和服务器之间建立了连接,因为
console.log(
Connected-tp${socket.id}
每次都会显示不同的套接字id,但在我的组件上,当我调用
emit
函数时,什么也不会发生:
window.socket.emit('joinRoom',{email:this.email,apike:this.apikey})

我无法在客户端接收事件消息,我已经测试了服务器,在普通浏览器上一切正常,但在我的电子应用程序上无法发出或接收消息

这与我的电子应用有关吗?

以下是我是如何做到的-

服务器端:

const express = require('express')
const app = express()

// middlewares
app.use(express.static('public'))

// routes
app.get('/', (req, res) => {
    res.render('index')
})

server = app.listen(7575, () => {
    console.log("Server started");
})

//socket.io instantiation
const io = require("socket.io")(server)

//listen on every connection
io.on('connection', (socket) => {
    console.log('New user connected');

    //listen on "test"
    socket.on('test', (data) => {
        var username = data.username;
    })
})
客户端:

socket = io.connect('http://localhost:7575')
socket.emit('test', {username: 'username'})

但它在Electron应用程序中起作用吗?我在这里发现这并不那么简单。