gpt4 book ai didi

node.js - Node js + socket io 仍然在客户端显示错误,如何修复? CentOS7

转载 作者:太空宇宙 更新时间:2023-11-03 23:00:14 29 4
gpt4 key购买 nike

首先我安装 Node.js

访问应用程序目录

cd /home/admin/web/my-domain.com/public_html

下载文件

wget https://nodejs.org/dist/v8.11.1/node-v8.11.1-linux-x64.tar.xz

提取文件

tar xvf node-v8.11.1-linux-x64.tar.xz

重命名为nodejs

mv node-v8.11.1-linux-x64 nodejs

安装

mkdir ~/bin
cp nodejs/bin/node ~/bin
cd ~/bin
ln -s ../nodejs/lib/node_modules/npm/bin/npm-cli.js npm

安装之间没有任何错误,然后检查版本

$ node --version

获取v6.3.1

$ npm --version

获取3.10.3

前往主目录

cd

………………

然后安装express

访问应用程序目录

cd /home/admin/web/my-domain.com/public_html

安装express(版本4.16.3)

npm install express

安装之间没有任何错误,转到主目录

cd

………………

然后安装socket io

访问应用程序目录

cd /home/admin/web/my-domain.com/public_html

安装 express

npm install socket.io

安装之间没有任何错误,转到主目录

cd

......................................

创建application.js

var express = require('express');
var app = express();
var server = app.listen(9999);
var io = require('socket.io').listen(server);

console.log("server start");
var name = {};
io.sockets.on('connection', function(socket) {
var ipv4 = socket.request.socket.remoteAddress;

socket.on('sendMsg', function(data) {
if (data.message === '' || data.message === null) {
return;
}else if(data.message.indexOf('<')>=0){
socket.disconnect();
return;
}
if (name[socket.id] !== null) {
var msg = name[socket.id] + "(" + ipv4 + ") : " + data.message.trim();
console.log(msg);
io.sockets.emit('sendMsg', {message: msg});
}
});
socket.on('sendName', function(data) {
if (data.name === "" || data.name === null) {
socket.disconnect();
} else {
console.log(data.name.trim() + "(" + ipv4 + ") 's connected");
name[socket.id] = data.name;
io.sockets.emit('sendName', {name: data.name.trim() + "(" + ipv4 + ") 's connected"});
}
});
socket.on('disconnect', function() {
console.log(name[socket.id] + " was disconnected");
io.sockets.emit('disconnected', {name: name[socket.id]});
});
});

......................................

创建index.php

<html>
<head>
<meta charset="UTF-8">
<title></title>
<script type="text/javascript" src="https://www.my-domain.com/node_modules/socket.io-client/dist/socket.io.js"></script>
<script src="https://code.jquery.com/jquery-1.9.1.js"></script>

</script>
<script>
$(document).ready(function() {
$('#chatboard').text("");
var name = prompt("Please enter your name?", "");
wsUri = 'https://' + (document.location.host) + ':9999';
var socket = io.connect(wsUri);
socket.emit('sendName', {name: name.trim()});
socket.on('sendName', function(data) {
$('#chatboard').append(data.name.trim() + "\r\n");
});
socket.on('sendMsg', function(data) {
$('#chatboard').append(data.message.trim() + "\r\n");
});
socket.on('disconnected', function(data) {
$('#chatboard').append(data.name.trim() + "'s Disconnect\r\n");
});
$('#messagebox').keypress(function(evt) {
if (event.which === 13) {
var message = $('#messagebox').val();
$('#messagebox').val("");
socket.emit('sendMsg', {message: message});
}
});
$('#button').click(function() {
var message = $('#messagebox').val();
$('#messagebox').val("");
socket.emit('sendMsg', {message: message});
});

});

</script>
</head>
<body>
<div class="chat-room">
<table>
<tr>
<td colspan="2">
<textarea id="chatboard" readonly="readonly" rows='10' cols='50' style='resize:none;'>

</textarea>
</td>
</tr>
<tr>
<td>
<input type='text' id='messagebox' maxlength="100" style='width:100%;'/>
</td>
<td>
<input type='button' id="button" value='Send'/>
</td>
</tr>
</table>
</div>
</body>
</html>

......................................

运行应用程序

访问应用程序目录

cd /home/admin/web/my-domain.com/public_html

运行应用程序

node application.js

这就是表演

server start

没有任何错误。

......................................

在浏览器中打开https://www.my-domain.com

显示设置名称对话框,输入名称

然后填写消息并按发送消息。

它会显示这样的错误

enter image description here

我该如何修复它。

最佳答案

对于 https,您需要 ssl 证书才能允许握手连接。

这就是我所做的,

var socket = require( 'socket.io' );
var app = require('express')();
var https = require('https');
var fs = require('fs');
var options = {key: fs.readFileSync('domain.key'),cert: fs.readFileSync('domain.crt'),ca : fs.readFileSync('domain.crt')};
var server = https.createServer(options,app);
var io = socket.listen( server );

var port = 8443;
server.listen(port,(res)=>{
console.log('Socket listening on port : '+port)
});

关于node.js - Node js + socket io 仍然在客户端显示错误,如何修复? CentOS7,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50157347/

29 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com