I'm trying to make real time application with node.js and socket.io. As I can see the server can see when new user connects but can't return information to client side or something. This is what I've on client side:
我正在尝试使用node.js和socket.io进行实时应用。正如我所看到的,服务器可以看到新用户何时连接但无法将信息返回给客户端或其他什么。这就是我在客户端:
<script src="<?= base_url('assets/js/socket.io.js') ?>"></script>
<script>
var socket;
socket = io('http://***.***.***.***:3030', {query: "key=key"});
socket.on('connect', function (data) {
console.log('Client side successfully connected with APP.');
});
socket.on('error', function (err) {
console.log('Error: ' + err);
});
</script>
and this is the server side:
这是服务器端:
var app = require("express")();
var http = require("http").createServer(app);
var io = require("socket.io")(http);
http.listen(3030, function () {
globals.debug('Server is running on port: 3030', 'success');
});
io.set('authorization', function (handshakeData, accept) {
var domain = handshakeData.headers.referer.replace('http://', '').replace('https://', '').split(/[/?#]/)[0];
if ('www.****.com' == domain) {
globals.debug('New user connected', 'warning');
} else {
globals.debug('Bad site authentication data, chat will be disabled.', 'danger');
return accept('Bad site authentication data, chat will be disabled.', false);
}
});
io.use(function (sock, next) {
var handshakeData = sock.request;
var userToken = handshakeData._query.key;
console.log('The user ' + sock.id + ' has connected');
next(null, true);
});
and when someone comes to website I'm expecting to see in console output "New user connected" and I see it: screen shot and the user should see on the browser console output: "Client side successfully connected with APP." but I doesn't show. Also I tried to emit data to user but it doesn't work too. I can't see any errors or something. This is not the first time I'm working with sockets but the first time facing such as problem. Maybe there is any error reporting methods to handle errors or something? Also I can't see output on io.use(....)
method
当有人来到网站时,我希望在控制台输出中看到“新用户连接”,我看到了:屏幕截图,用户应该在浏览器控制台输出上看到:“客户端与APP成功连接。”但我没有表现出来。此外,我尝试向用户发送数据,但它也不起作用。我看不出任何错误或其他什么。这不是我第一次使用套接字,而是第一次遇到问题。也许有任何错误报告方法来处理错误或什么?我也看不到io.use(....)方法的输出
1 个解决方案
#1
1
The solution is to pass "OK" sign just after authenticating to do the next method:
解决方案是在验证后立即传递“OK”符号以执行下一个方法:
io.set('authorization', function (handshakeData, accept) { var domain = handshakeData.headers.referer.replace('http://', '').replace('https://', '').split(/[/?#]/)[0];
io.set('authorization',function(handshakeData,accept){var domain = handshakeData.headers.referer.replace('http://','')。replace('https://','')。分裂(/ [/#?] /)[0];
if ('www.****.com' == domain) {
globals.debug('New user connected', 'warning');
accept(null, true);
} else {
globals.debug('Bad site authentication data, chat will be disabled.', 'danger');
return accept('Bad site authentication data, chat will be disabled.', false);
}
});
#1
1
The solution is to pass "OK" sign just after authenticating to do the next method:
解决方案是在验证后立即传递“OK”符号以执行下一个方法:
io.set('authorization', function (handshakeData, accept) { var domain = handshakeData.headers.referer.replace('http://', '').replace('https://', '').split(/[/?#]/)[0];
io.set('authorization',function(handshakeData,accept){var domain = handshakeData.headers.referer.replace('http://','')。replace('https://','')。分裂(/ [/#?] /)[0];
if ('www.****.com' == domain) {
globals.debug('New user connected', 'warning');
accept(null, true);
} else {
globals.debug('Bad site authentication data, chat will be disabled.', 'danger');
return accept('Bad site authentication data, chat will be disabled.', false);
}
});