如何在电子应用程序中实现Socket.IO? [英] How do I implement Socket.IO in an electron app?
问题描述
我想在Electron应用程序中实现Socket.IO,但是我找到了否文档和否示例。
如果有人可以向我解释两个或多个客户如何通过电子应用程序进行通信,我将不胜感激!
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!
推荐答案
您知道,电子应用程序将在最终用户处运行。
因此,您应该在类似Cloud服务器的某处创建Socket服务器,并且您的电子应用应包含一个socket.io客户端实例。
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.
在Socket服务器上
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>
或
// with ES6 import
import io from 'socket.io-client';
const socket = io('http://localhost');
以便用户可以在您的Electron应用程序内进行通信。
So that users can communicate inside your Electron app.
这篇关于如何在电子应用程序中实现Socket.IO?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!