请帮助我,非常感谢!!!!
在我的本地主机中,它确实走得很好,但是现在我把所有东西都放在服务器上,以查看它如何在线工作,并且有问题。
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
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' }
信息服务器
我不知道如何解决此错误
这是我的服务器代码-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);
}
}
});
这是我的客户代码
$(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();
}
}
});
});
Redis连接
'redis' => [
'class' => 'yii\redis\Connection',
'hostname' => 'localhost',
'port' => 6379,
'database' => 0
],