How do I implement Socket.IO in an electron app?

不羁岁月 提交于 2020-07-19 13:55:28

问题


I want to implement Socket.IO in an Electron app, however I have found no documentation and no examples of how this could work. If someone could explain to me how two or more clients could communicate via the electron app, I would be very grateful!


回答1:


You know, the electron app will be running at end user. So you should create Socket server at somewhere sth like Cloud server and your electron app should contain one socket.io client instance.

At Socket server

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

const server = require('http').createServer(app);
const io = require('socket.io')(server);
io.on('connection', () => { /* … */ });
server.listen(3000);

And at frontend (your case Electron app side)

<script src="/socket.io/socket.io.js"></script>
<script>
  var socket = io('http://localhost');
  socket.on('connect', function(){});
  socket.on('event', function(data){});
  socket.on('disconnect', function(){});
</script> 

or

// with ES6 import
import io from 'socket.io-client';
 
const socket = io('http://localhost');

So that users can communicate inside your Electron app.



来源:https://stackoverflow.com/questions/60333120/how-do-i-implement-socket-io-in-an-electron-app

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!