我想在我的应用程序中使用Socket.io添加实时通知,但我在将其集成到我的应用程序中时遇到了问题。当我运行应用程序并查看浏览器日志(控制台)时,
我拒绝连接
(GET http://localhost:1500/socket.io/?EIO=3&transport=polling&t=1434954742052-24 净:: ERR_CONNECTION_REFUSED)
我的应用程序位于localhost:130。我已经阅读了CORS并在我的控制器中应用了持久性。
header('Access-Control-Allow-Origin: *');
header("Access-Control-Allow-Headers: X-API-KEY, Origin, X-Requested-With,
Content - Type, Accept, Access - Control - Request - Method ");
header("Access-Control-Allow-Methods: GET, POST, OPTIONS, PUT, DELETE");
$method = $_SERVER['REQUEST_METHOD'];
if ($method == "OPTIONS") {
die();
}
parent::__construct();
这是我的server.js
var io = require('socket.io').listen(1500);
io.sockets.on('connection', function (socket) {
// Let everyone know it's working
socket.emit('startup', { message: 'I Am Working!!' });
});
这是我的client.js
$(function() {
window.MY_Socket = {
// Instantiate the Socket.IO client and connect to the server
socket: io.connect('http://localhost:1500'),
// This just indicates that a Socket.IO connection has begun.
startupMessage: function(data) {
console.log(data.message);
},
}
});
我仍然收到error:net::ERR_CONNECTION_REFUSED
。我一直在寻找工作,但我仍然可以想办法解决这个问题。期待你的帮助
答案 0 :(得分:0)
感谢@DamirMiladinov帮助我,下面是代码。
var express = require('express');
var app = express();
var http = require('http').Server(app);
var io = require('socket.io')(http);
app.use(express.static(__dirname));
var onlines = 0
var users = []
var chat = ''
io.on('connection', function(socket){
console.log('[!] Um jogador conectou-se.');
onlines += 1
io.emit('updateonlines', [onlines]);
socket.emit('recvmessage', [chat]);
socket.on('disconnect', function(){
console.log('[!] Um jogador desconectou-se.');
onlines -= 1
io.emit('updateonlines', [onlines]);
});
});
[...]