socket.io无法加载资源:net :: ERR_TIMED_OUT

时间:2019-01-30 14:43:36

标签: sockets websocket socket.io

我正在用socket.io和yiiy进行测试,但是我遇到以下问题

GET https://mydomain.io:8890/socket.io/?userid=5b344be12820260db552b202&room=5c347250ac210c52bc542f12&EIO=3&transport=polling&t=MYV9V2s net::ERR_TIMED_OUT

无法加载资源:net :: ERR_TIMED_OUT

我不知道如何解决此错误

这是我的服务器代码-server.js

var crypto = require('crypto');
var fs = require("fs");
var https = require("https");
var Redis = require('ioredis');
var redis = new Redis();
var users = {};

var options = {
    key: fs.readFileSync('/etc/letsencrypt/live/mydomain.io/privkey.pem'),
    cert: fs.readFileSync('/etc/letsencrypt/live/mydomain.io/fullchain.pem')
};


var express = require("express");
var app = express();


var server = https.createServer(options, app).listen(8890, function () {
    console.log("Express server listening on port " + 8890);
});

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

io.on('connection', function (socket) {
    console.log("New connect");
    var userId = socket.handshake.query.userid;
    var room = socket.handshake.query.room;
    socket.join(room);
    if (userId in users) {
        //fail
        console.log('Already exist');
    } else {
        console.log("Added");
        users[userId] = socket;
    }

    //disconnect
    socket.on('disconnect', function () {
        console.log(socket.id + ': disconnect');
        delete users[userId];
    });

});

redis.subscribe('chat', 'focus', function (err, count) {
    // Now we are subscribed to both the 'news' and 'music' channels.
    // `count` represents the number of channels we are currently subscribed to.
});

redis.on("message", function (channel, message) {
    var message = JSON.parse(message);
    console.log(message);
    if (channel == 'chat') {
        var receive = message.receive;
        var sender = message.sender;
        var senderSocket = users[sender] || null;
        var receiveSocket = users[receive] || null;
        if (receiveSocket) {
            receiveSocket.join(message.message_id);
        }
        if (senderSocket) {
            senderSocket.join(message.message_id);
//        io.to(users[message.receive].id).emit(channel, message);
            io.to(message.message_id).emit(channel, message);
            console.log("Message was succefully sent!");
        }
    } else if (channel == 'focus') {
        var senderSocket = users[message.sender] || null;
        if (senderSocket) {
            senderSocket.broadcast.to(message.message_id).emit(channel, message);
        }
    }
});

这是我的客户代码

    <script>jQuery(function ($) {

    scrollBottom();
    $('.slider-single').slick({
        slidesToShow: 1,
        slidesToScroll: 1,
        arrows: false,
        fade: true,
        asNavFor: '.slider-nav'
    });
    $('.slider-nav').slick({
        slidesToShow: 5,
        slidesToScroll: 1,
        asNavFor: '.slider-single',
        dots: true,
        centerMode: true,
        focusOnSelect: true
    });
    lightbox.option({
        'resizeDuration': 200,
        'wrapAround': true
    });
    $("#chat-form").submit(function () {
        if ($("#input-message").val() != '') {
            $.ajax({
                url: $(this).attr('action'),
                type: "POST",
                data: $(this).serialize(),
                success: function (data) {
                    $("#input-message").val('');
                }
            });
        }
        return false;
    });
    $('#input-message').focusin(function () {
        $.ajax({
            url: '/site/focus',
            type: "POST",
            data: "focus=in&message=" + "5c347250ac210c52bc542f12",
            success: function (data) {

            }
        });
        return false;
    });
    $('#input-message').focusout(function () {
        $.ajax({
            url: '/site/focus',
            type: "POST",
            data: "focus=out&message=" + "5c347250ac210c52bc542f12",
            success: function (data) {

            }
        });
        return false;
    });
    $(document).on('click', '.no-read a', function () {
        var url = $(this).attr('href');
        var id = $(this).attr('data-id');
        $.ajax({
            url: '/site/read',
            type: "POST",
            data: "id=" + id,
            success: function (data) {
                window.location.href = url;
            }
        });
    });
    $(document).ready(function () {
        var userid = '5b344be12820260db552b202';//anything you like
        var room = '5c347250ac210c52bc542f12';//anything you like
        var socket = io.connect('https://mydomain.io:8890', {query: "userid=" + userid + "&room=" + room});
        socket.on('chat', function (data) {
            console.log('123455');
            var owner = '5b344be12820260db552b202';
            $("#last-msg-" + data.message_id).html(data.message);
            var clone_elem = $('#' + data.message_id).clone();
            $('#' + data.message_id).remove();
            if (owner == data.sender) {
                $("#new-message-" + data.message_id).append('<div class="line pull-right clearfix"><span>' + data.message + '</span></div>');
            } else {
                $("#new-message-" + data.message_id).append('<div class="line pull-left clearfix"><span>' + data.message + '</span></div>');
                if (data.message_id != '5c347250ac210c52bc542f12') {
                    clone_elem.addClass('no-read');
                } else {
                    $.ajax({
                        url: '/site/read',
                        type: "POST",
                        data: "id=" + data.message_id,
                        success: function (data) {
                        }
                    });
                }
                document.getElementById("audio-" + data.receive).play();
            }
            $('ul.list-group').prepend(clone_elem);
            scrollBottom();
        });
        socket.on('focus', function (data) {
            if ('5c347250ac210c52bc542f12' == data.message_id) {
                if (data.message == 'in') {
                    $("#chatting").show();
                } else if (data.message == 'out') {
                    $("#chatting").hide();
                }
            }
        });
    });
    function scrollBottom() {
        var objDiv = document.getElementById("ms-content");
        objDiv.scrollTop = objDiv.scrollHeight;
    }

    $(function () {
        $(window).resize(function (e) {
            placeFooter();
        });
        placeFooter();
        scrollBottom();
        // hide it before it's positioned
//        $('#footer').css('display', 'inline');
    });
    function placeFooter() {
        var width = $(window).width();
        console.log(width);
        if (width == 360) {
            $('#ms-content').css('height', 412);
        } else if (width == 375) {
            $('#ms-content').css('height', 510);
        } else if (width == 411) {
            $('#ms-content').css('height', 510);
        } else if (width == 414) {
            $('#ms-content').css('height', 510);
        } else if (width == 768) {
            $('#ms-content').css('height', 600);
        } else {
            var height = $(window).height() - 175;
            $('#ms-content').css('height', height);
        }
    }
    $('body').on('click', '.tab-msg', function (e) {
        e.preventDefault();
        $(".list-msg").hide();
        $(".list-conver").show();
        $(".checkout").hide();
        $(".users-list").hide();
        $(".tab-product").removeClass("btn-success");
        $(".tab-product").addClass("btn-default");
        $(this).removeClass("btn-default");
        $(this).addClass("btn-success");
    });
    $('body').on('click', '.tab-product', function (e) {
        e.preventDefault();
        $(".list-msg").hide();
        $(".list-conver").hide();
        $(".checkout").show();
        $(".tab-msg").removeClass("btn-success");
        $(".tab-msg").addClass("btn-default");
        $(this).removeClass("btn-default");
        $(this).addClass("btn-success");
    });
    $('body').on('click', '.back', function (e) {
        e.preventDefault();
        $(".list-msg").show();
        $(".users-list").show();
        $(".list-conver").hide();
    });


});</script> 

连接数据库

'redis' => [
            'class' => 'yii\redis\Connection',
            'hostname' => 'localhost',
            'port' => 6379,
            'database' => 0
        ],

我使用此代码

<script src='https://cdnjs.cloudflare.com/ajax/libs/socket.io/2.0.3/socket.io.js'></script>

在我的本地主机中,它确实走得很好,但是现在我把所有东西都放在服务器上,以查看它如何在线工作,并且有问题。

debug = socket.io *节点server.js

[root@litespeed public_html]# debug=socket.io* node server.js
Express server listening on port 8890
{ message: 'in',
  sender: '5b344be12820260db552b202',
  message_id: '5c526142ac210c415f5422e2' }
{ message: 'sdasd',
  sender: '5b344be12820260db552b202',
  receive: '5c36f45fac210c6cd07f0b32',
  message_id: '5c526142ac210c415f5422e2' }

信息服务器

  • Npm版本6.5.0
  • 节点v 11.8.0
  • socket.io
  • redis-server 5.0.3 /端口6379

请帮助我,非常感谢!!!!

0 个答案:

没有答案