簡體   English   中英

Socket.io 404錯誤

[英]Socket.io 404 error

我遵循了Jeffrey Way的教程。
教程:
https://laracasts.com/series/real-time-laravel-with-socket-io/episodes/1

這是我的index.js

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

server.listen(3000);

app.get('/', function(request, response){   
   response.sendFile(__dirname+'/index.html'); 
});

io.on('connection', function(socket){   
   console.log('A connection is made'); 
});

有人說它的版本和年份。 我認為上面的代碼適用於2014年。

這是我的html:

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8"></meta>
    <title>Chat Lesson</title>
</head>
<body>
    <h1>Hello world!</h1>

    <script src="https://cdnjs.cloudflare.com/ajax/libs/socket.io/2.0.4/socket.io.js"></script>
    <script type="text/javascript">
        var socket = io();
    </script>
</body>
</html>

我做了很多研究。 有人說順序很重要,但是我發現了很多順序。

我的錯誤:

GET http://chat-lesson.local:8888/socket.io/?EIO=3&transport=polling&t=1515636281477-74 404 (Not Found)

我的問題:
為什么找不到404? 我在每一步都遵循了Jeffrey Way的教程,該教程很短,只有5至8分鍾。

  • Mamp Pro 4
  • NPM 3.10.10

我嘗試使用server.listen(8888);

events.js:160
      throw er; // Unhandled 'error' event
      ^

Error: listen EADDRINUSE :::8888
    at Object.exports._errnoException (util.js:1022:11)
    at exports._exceptionWithHostPort (util.js:1045:20)
    at Server._listen2 (net.js:1259:14)
    at listen (net.js:1295:10)
    at Server.listen (net.js:1391:5)
    at Object.<anonymous> (/Users/kendantinio/Documents/Freelancer/blvnp/chat-lesson/index.js:5:8)
    at Module._compile (module.js:570:32)
    at Object.Module._extensions..js (module.js:579:10)
    at Module.load (module.js:487:32)
    at tryModuleLoad (module.js:446:12)

您的服務器正在偵聽端口3000,但是顯示的socket.io URL是端口8888。如果您確實在端口8888上運行了其他服務器(也許是PHP服務器),則不是這台服務器正在運行socket.io。

為了確切地了解需要什么修復程序,這引發了一個問題,即顯示HTML的頁面的URL是什么? 顯示該頁面時,瀏覽器在URL欄中顯示什么? 您的node.js代碼表示您打算從端口3000的node.js服務器加載該頁面,但是如果出現這種情況,您顯示的客戶端代碼將不會從端口8888加載該頁面。

嘗試執行此代碼。

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

app.get('/', function(req, res) {
    res.sendfile('index.html');
});

users = [];
io.on('connection', function(socket) {
    console.log('A user connected');
    socket.on('setUsername', function(data) {
        console.log(data);

        if(users.indexOf(data) > -1) {
            socket.emit('userExists', data + ' username is taken! Try some other username.');
        } else {
            users.push(data);
            socket.emit('userSet', {username: data});
        }
    });

    socket.on('msg', function(data) {
        //Send message to everyone
        io.sockets.emit('newmsg', data);
    })
});

http.listen(3000, function() {
   console.log('listening on localhost:3000');
});

讓我知道它是否有效。

我要添加它是因為它首先出現在Google上,花了我兩天的時間才找到解決方案。

我正在使用Node.js和React,以及socket.io-client

我可以通過將url和配置對象傳遞給io名稱空間來使其工作。

import io from 'socket.io-client'
const socket = io('http://localhost:3000', {
    reconnectionDelay: 1000,
    reconnection: true,
    reconnectionAttemps: 10,
    transports: ['websocket'],
    agent: false,
    upgrade: false,
    rejectUnauthorized: false
});

我幾乎可以肯定Marlon的代碼能正常工作,因為http.listen位於index.js的末尾

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM