2017-04-09 74 views
0

斐伊川我有在節點js代碼 problam我運行在控制檯服務器的的NodeJS不顯示出任何問題,但 所有的時間我得到erorr控制檯從鉻:GET HTTP://localhost/socket.io/ EIO = 3&運輸=輪詢&T = LjIvLGU 404(未找到)

「 GET http://localhost/socket.io/?EIO=3&transport=polling&t=LjIvLGU 404(未找到)」

的index.html

<head> 
    <title>test</title> 
    <meta charset="UTF-8"> 
    <meta name="viewport" content="width=device-width, initial-scale=1.0"> 
    <script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.2.1/jquery.min.js"></script> 
    <script src="http://localhost:3000/socket.io/socket.io.js"></script> 

</head> 

的script.js

var express = require('express'); 
var app = express(); 
var server = require('http').Server(app); 
var io = require('socket.io').listen(server); 

users = []; 
connections = []; 

server.listen(process.env.PORT || 3000); 

console.log("server up"); 

app.get('/', function (req, res) { 
    res.sendFile(__dirname + '/index.php'); 
}); 

io.sockets.on('connection', function (socket) { 

    socket.on('subscribe', function (data) { 
     socket.join(data.room); 
    }); 
    socket.on('arriv', function (data) { 
     console.log(data); 
     io.sockets.in('invitor').emit('send message',data); 
    }); 

    socket.on('unsubscribe', function (data) { 
     socket.leave(data.room); 
    }); 

}); 

user.js的

$(function() { 
     var cid = $.session.get('company'); 
     console.log(cid); 

     var socket = io.connect(); 

     socket.emit("subscribe", {room: "invitor"}); 

     socket.on("send message", function (data){ 
      $('#info').append("" + data.gid + "</br>"); 
     }); 
     $(document).on('click', '.123', function() { 
      var gid = $(this).attr('id'); 
      socket.emit('arriv', {gid: gid}); 
     }); 
    }); 

回答

1

看來你正試圖在本地主機其他地方連接您的插座不端口3000。如果我測試你的代碼片段一切正常。您的錯誤消息告訴我,端口3000丟失" GET http://localhost/socket.io/?EIO=3&transport=polling&t=LjIvLGU 404 (Not Found)"。如果您將io.connect更改爲var socket = io.connect('http://localhost:3000');

相關問題