Socket.IO 与 Electron 应用程序将前端数据发送到后端?
我看过很多关于如何实现 socket.io 的示例,但我找不到任何一个可以在此过程中维护电子应用程序的结构(通常只是使用 Express 来代替)。我想知道如何使用基本的电子应用程序来实现 socket.io?这是我的样板代码:
Client.js 文件:
const socket = io("http://localhost:3000");
// When client successfully connects to server
socket.on("connect", () => {
console.log(`Connected to server`);
});
// Receive message from backend
socket.on("message", data => {
console.log(`Server: ${data}`);
});
如果可能的话,我希望在我的电子代码中接收上述 socket.on
操作(同时将内容保留在应用程序内部,而不是删除应用程序代码和通过打开浏览器来执行此操作)
Electron.js 样板:
const { app, BrowserWindow } = require('electron');
const path = require('path');
// Handle creating/removing shortcuts on Windows when installing/uninstalling.
if (require('electron-squirrel-startup')) {
// eslint-disable-line global-require
app.quit();
}
const createWindow = () => {
// Create the browser window.
const mainWindow = new BrowserWindow({
width: 800,
height: 600,
});
// and load the index.html of the app.
mainWindow.loadFile(path.join(__dirname, 'index.html'));
// Open the DevTools.
mainWindow.webContents.openDevTools();
};
// This method will be called when Electron has finished
// initialization and is ready to create browser windows.
// Some APIs can only be used after this event occurs.
app.on('ready', createWindow);
app.on('window-all-closed', () => {
if (process.platform !== 'darwin') {
app.quit();
}
});
app.on('activate', () => {
if (BrowserWindow.getAllWindows().length === 0) {
createWindow();
}
});
感谢任何帮助!
I've seen a lot of examples on how to implement socket.io, but none that I can find that maintain the structure of the electron app in the process (typically it's just using express instead). I was wondering how I could implement socket.io with a basic electron app? Here's my boiler plate code:
Client.js file:
const socket = io("http://localhost:3000");
// When client successfully connects to server
socket.on("connect", () => {
console.log(`Connected to server`);
});
// Receive message from backend
socket.on("message", data => {
console.log(`Server: ${data}`);
});
I'd like to receive the above socket.on
actions in my electron code if possible (while keeping things inside the app as opposed removing the app code and doing this by opening the browser)
Electron.js boilerplate:
const { app, BrowserWindow } = require('electron');
const path = require('path');
// Handle creating/removing shortcuts on Windows when installing/uninstalling.
if (require('electron-squirrel-startup')) {
// eslint-disable-line global-require
app.quit();
}
const createWindow = () => {
// Create the browser window.
const mainWindow = new BrowserWindow({
width: 800,
height: 600,
});
// and load the index.html of the app.
mainWindow.loadFile(path.join(__dirname, 'index.html'));
// Open the DevTools.
mainWindow.webContents.openDevTools();
};
// This method will be called when Electron has finished
// initialization and is ready to create browser windows.
// Some APIs can only be used after this event occurs.
app.on('ready', createWindow);
app.on('window-all-closed', () => {
if (process.platform !== 'darwin') {
app.quit();
}
});
app.on('activate', () => {
if (BrowserWindow.getAllWindows().length === 0) {
createWindow();
}
});
Any help is appreciated!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我将分享我的工作示例:
Server.ts
Client.ts
并在 main.ts 中启动服务器
在 server.ts 中,您将看到
origin: "http://localhost:1212"
这是要避免的 CORS发出警告。端口1212
是在我的 Electron 应用程序中用作 http 服务器端口的号码。详细信息 https://socket.io/docs/v4/handling-corsI will share my working example:
Server.ts
Client.ts
And started server in main.ts
In server.ts you will see
origin: "http://localhost:1212"
this is CORS to avoid issues warnings. Port1212
is number that is used in my Electron app as http server port. Details https://socket.io/docs/v4/handling-cors