【发布时间】:2022-12-19 18:06:56
【问题描述】:
我有一个工作的 websocket 服务器。我以前在网络浏览器/反应中使用 websocket 作为客户端,但我无法在电子应用程序中使用 Websocket,因为 WebSocket 取决于浏览器的兼容性,并且出于某种原因,此功能在 Electron 中不可用。
I use nextron (nextjs/react + electron) boilerplate.
yarn create nextron-app MY_APP --example with-typescript-material-ui
import React from 'react';
import Head from 'next/head';
import { ThemeProvider } from '@material-ui/core/styles';
import CssBaseline from '@material-ui/core/CssBaseline';
import { theme } from '../lib/theme';
import type { AppProps } from 'next/app';
export default function (props: AppProps) {
const { Component, pageProps } = props;
// where to put ws here ? this placement generates an error
const ws = new WebSocket("ws://192.168.100.8:8081/")
console.log("file: _app.tsx:11 ~ ws", ws)
React.useEffect(() => {
const jssStyles = document.querySelector('#jss-server-side');
if (jssStyles) {
jssStyles.parentElement.removeChild(jssStyles);
}
}, []);
return (
<React.Fragment>
<Head>
<meta name="viewport" content="minimum-scale=1, initial-scale=1, width=device-width" />
</Head>
<ThemeProvider theme={theme}>
<CssBaseline />
<Component {...pageProps} />
</ThemeProvider>
</React.Fragment>
);
}
【问题讨论】:
标签: javascript reactjs websocket electron