如何在我的Node应用中启动一个简单的WebSocket

huangapple go评论54阅读模式
英文:

How can I get a simple Web socket up and running in my Node app

问题

I have a very basic Node.js app running on my local machine, accessible at http://localhost:3000/. All working fine.

我在本地机器上运行了一个非常基本的Node.js应用程序,可以通过http://localhost:3000/访问。一切都正常。

I want to add a web socket server to the app and then to make a connection to it from a local html page.

我想向应用程序添加一个WebSocket服务器,然后从本地HTML页面连接到它。

I have followed code examples from the internet but am having no luck. I never see the console.log that I expect to see on the 'connection' event.

我已经遵循了来自互联网的代码示例,但没有成功。我从未看到我期望在“connection”事件上看到的console.log。

In app.js I have the following code:

在app.js中,我有以下代码:

var WebSocketServer = require('ws').Server;

My existing app.listen call looks like this:

我的现有app.listen调用如下:

const port = process.env.PORT || 3000;
var m_server = app.listen(port, () => {
    console.log("Server online on http://localhost:" + port);
});

I use the return value of the above in the following call:

我在以下调用中使用了上述代码的返回值:

var m_webSocketServer = new WebSocketServer( { server: m_server, autoAcceptConnections: true} );

m_webSocketServer.on('connection', function (ws) {
    console.log("A connection");
});

console.log("At End");

这是服务器端的内容。

In my client web page body I have in the onload event:

在我的客户端网页主体中,我在onload事件中有以下内容:

alert("Here");
this.m_websocketConnection = new WebSocket('wss://localhost:3000');
alert(this.m_websocketConnection);

I had hoped to see the "A connection" message from the server side, I do not, I just see "At End" followed by "Server online on http://localhost:3000" in the console.

我希望从服务器端看到“A connection”的消息,但实际上没有看到,我只在控制台中看到“At End”后面跟着“Server online on http://localhost:3000”。

and in the browser I see "Here" and "[object WebSocket]"

在浏览器中,我看到“Here”和“[object WebSocket]”。

Can anyone help me out? Thanks.

有人可以帮助我吗?谢谢。

英文:

I have a very basic Node.js app running on my local machine, accessible at http://localhost:3000/ All working fine.

I want to add a web socket server to the app and then to make a connection to it from a local html page.

I have followed code examples from the internet but am having no luck. I never see the console.log that I expect to see on the 'connection' event.

In app.js I have the following code:

var WebSocketServer = require('ws').Server;

My existing app.listen call looks like this:

const port = process.env.PORT || 3000;
var m_server = app.listen(port, () => {
    console.log("Server online on http://localhost:" + port);
});

I use the return value of the above in the following call:

var m_webSocketServer = new WebSocketServer( { server: m_server, autoAcceptConnections: true} );

m_webSocketServer.on('connection', function (ws)
                        {
                            console.log("A connection");
                        }
                    );

console.log("At End");

That's it on the server side.

In my client web page body I have in the onload event:

alert("Here");
this.m_websocketConnection = new WebSocket('wss://localhost:3000');
alert(this.m_websocketConnection);

I had hoped to see the "A connection" message from the server side, I do not, I just see "At End" followed by "Server online on http://localhost:3000" in the console.

and in the browser I see "Here" and "[object WebSocket]"

Can anyone help me out? Thanks.

答案1

得分: 1

Update: 在我提出这个问题不到一分钟后,我找到了问题!

在客户端这边,我将:

new WebSocket('wss://localhost:3000');

更改为:

new WebSocket('ws://localhost:3000');

谢谢。

英文:

Update: less that a minute after composing this question I found the problem!

On the client side I changed:

new WebSocket('wss://localhost:3000');

to

new WebSocket('ws://localhost:3000')

Thanks.

huangapple
  • 本文由 发表于 2023年3月31日 23:27:54
  • 转载请务必保留本文链接:https://go.coder-hub.com/75900243.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定