节点js + socket io仍然在客户端显示错误,如何修复它? centos7

时间:2018-05-03 14:20:17

标签: node.js express websocket socket.io

首先我安装节点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

去主dir

cd

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

然后安装快递

访问应用程序目录

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

安装快递

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

我该如何修复它。

2 个答案:

答案 0 :(得分:2)

对于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)
});

答案 1 :(得分:1)

您可以使用此代码

var app = require('express')();
var server = require('http').Server(app);
var io = require('socket.io')(server);

server.listen(80);

app.get('/', function (req, res) {
  res.sendfile(__dirname + '/index.html');
});

io.on('connection', function (socket) {
  socket.emit('news', { hello: 'world' });
  socket.on('my other event', function (data) {
    console.log(data);
  });
});

来自Socket.io -Using with Express 3/4