简体   繁体   English

我无法启动我的本地主机 websocket 服务器

[英]I can't launch my localhost websocket server

I am trying to create a localhost websocket server with node 16.13 and I don't know why but I nothing seems to work.我正在尝试使用节点 16.13 创建一个 localhost websocket 服务器,但我不知道为什么,但似乎没有任何效果。 I can't launch my index.js with node index.js, I receive this error message -> Warning: To load an ES module, set "type": "module" in the package.json or use the .mjs extension.(Use node --trace-warnings ... to show where the warning was created)我无法使用节点 index.js 启动我的 index.js,我收到此错误消息 -> Warning: To load an ES module, set "type": "module" in the package.json or use the .mjs extension.(Use node --trace-warnings ... to show where the warning was created)

Also this message in the browser Access to script at 'file:///.../server/index.js' from origin 'null' has been blocked by CORS policy: Cross origin requests are only supported for protocol schemes: http, data, chrome, chrome-extension, chrome-untrusted, https.此外,浏览器中的这条消息Access to script at 'file:///.../server/index.js' from origin 'null' has been blocked by CORS policy: Cross origin requests are only supported for protocol schemes: http, data, chrome, chrome-extension, chrome-untrusted, https.

In my html file I've used <script type="module" src="./server/index.js"></script>在我的 html 文件中,我使用了<script type="module" src="./server/index.js"></script>

(index.js file) 
import WebSocket from 'ws';
    
const wss = new WebSocket.Server({ port: 8080});

const ws = new WebSocket("ws://localhost:8080");

ws.addEventListener("open", () => {
    console.log("We're connected")

    ws.send("Hey, how is going?");
});

ws.on("connection", ws => {
    console.log("New client Connected");

    ws.on("message", data => {
        console.log(`Client has sent Us: ${data}`);
    });
    
});

ws.on("close", () => {
    console.log("client has disconnected");
});```

You are importing your module with ESM syntax and nodejs only support CommonJs您正在使用 ESM 语法导入模块,而 nodejs 仅支持 CommonJs

Change改变

import WebSocket from 'ws';

For:为了:

const ws = required('ws')

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 我无法连接到本地主机上的服务器 - I Can't connect to my server on localhost 如何在实时服务器而不是本地主机上使用 Tornado 实现 WebSocket - how can I implement a WebSocket using Tornado on a live server instead of localhost 无法连接到Websocket服务器 - Can't connect to the websocket server 我可以将 websocket 连接到非 websocket 服务器吗 - Can I connect a websocket to a non websocket server 为什么我不能在运行由Openshift托管的Node.js + Websocket的服务器上获取连接客户端的IP地址(使用WebSocket-Node) - Why can't I get the connecting client's ip address on my server running Node.js + Websocket hosted by Openshift (Using WebSocket-Node) 如何使用 javascript 从我的网站查询本地主机服务器? - How can I query localhost server from my website with javascript? 我无法连接到我的 websocket,因为它的握手 Sec-WebSocket-Accept 标头值不正确 - I can't connect to my websocket because its handshake Sec-WebSocket-Accept header value is incorrect 为什么我不能在html上启动我的LibGDX项目? - Why can't I launch my LibGDX project on html? 检查WebSocket服务器是否正在运行(在localhost上) - Check if WebSocket Server is running (on localhost) Websocket 服务器和 npm 在本地主机上启动 - Websocket server and npm start on localhost
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM