好吧,正如我所说的那样,当我转到主页时,套接字连接可以正常工作,但是当我使用路由时,它根本不起作用,这就是我的index.js
io.of('/admin').on('connection', function(socket) {
console.log('made socket connection', socket.id);
console.log(socket.request.user);
socket.on('chat', async function(data) {
console.log(data);
client.guilds.channels.get('474951005788962846').send(data);
io.sockets.emit('chat', data);
});
});
io.on('connection', function(socket) {
console.log('made socket connection', socket.id);
console.log(socket.request.user);
socket.on('chat', async function(data) {
console.log(data);
client.guilds.channels.get('474951005788962846').send(data);
io.sockets.emit('chat', data);
});
});
在我的/ admin页面上,这是我的脚本,该脚本是相同的,但是与我添加/ admin时的连接不是
<script>
var socket = io.connect('https://domain/admin');
// Query DOM
var serverID = document.getElementById('add_server_id');
var serverRoles = document.getElementById('add_role_ids');
var btnServer = document.getElementById('add_server_save');
//var output = document.getElementById('output');
// Emit events
btnServer.addEventListener('click', function(){
socket.emit('chat', {
serverid: serverID.value,
serverroles: serverRoles.value
});
});
//Listen for events
socket.on('chat', function(data) {
console.log(data);
//output.innerHTML += '<p><strong>' + data.game + '</strong></p>';
});
</script>
如果有人可以告诉我为什么它没有连接,我将非常感谢
答案 0 :(得分:0)
var socket = io.connect('https://domain/admin');
到
var socket = io.connect('/admin');
名称空间是Socket.IO协议的实现细节, 并且与基础传输的实际网址无关, 默认为/socket.io/…。