[英]Connect to a third party socket.io server
I'm creating a socket.io server like so: 我正在创建一个socket.io服务器,如下所示:
var http = require('http');
var io = require('socket.io');
var port = 8080;
var server = http.createServer(function(req, res){
res.writeHead(200,{ 'Content-Type': 'text/html' });
res.end('<h1>Hello Socket Lover!</h1>');
});
server.listen(port);
// Create a Socket.IO instance, passing it our server
var socket = io.listen(server);
// Add a connect listener
socket.on('connection', function(client){
console.log('Connection to client established');
// Success! Now listen to messages to be received
client.on('message',function(event){
console.log('Received message from client!',event);
});
client.on('disconnect',function(){
clearInterval(interval);
console.log('Server has disconnected');
});
});
console.log('Server running at http://127.0.0.1:' + port + '/');
The server works fine and starts, however when I'm connecting through js like this: 服务器工作正常并启动,但是当我通过js连接时,如下所示:
$(function(){
var socket = io();
socket.connect('http://localhost:8080');
});
It's not connecting and I'm getting this in dev tools console. 它没有连接,我正在开发工具控制台中找到它。
polling-xhr.js:264 GET http://file/socket.io/?EIO=3&transport=polling&t=Lz53lhL net::ERR_NAME_NOT_RESOLVED
I'm loading socket.io.js like this: 我正在像这样加载socket.io.js:
<script src="http://127.0.0.1:8080/socket.io/socket.io.js"></script>
Change 更改
$(function(){
var socket = io();
socket.connect('http://localhost:8080');
});
to 至
$(function(){
var socket = io('http://localhost:8080');
});
You need to pass the url of your socket server to the io
function 您需要将套接字服务器的URL传递给
io
函数
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.