2016-06-26 2 views
0

우리는 nodejs와 socket.io를 서버로 사용하는 어떤 종류의 채팅 앱을 가지고 있습니다. 클라이언트는 단지 PHP 기반의 일반적인 앱입니다. 그것은 아주 잘 작동하고 있습니다.Socket.io는 nonsocket.io URL을 위해 404를 제공합니다

그러나 기본 url, favicon.ico 및 나머지 URL과 같이 socket.io 앱과 관련되지 않은 URL에 404 오류를 제공하고 싶습니다.

예를 들어, 서버에 있습니다 :

https://awesome-messenger.com 

및 일반 socket.io URL이에있다 : 우리는 소켓에 의해 처리되지 아무것도에 404을 던져 싶습니다

https://awesome-messenger.com/socket.io/* 

. io.

현재 설정 :

  • nodejs 6.x에서
  • socket.io 최신

샘플 코드 (messenger.js) :

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

app.listen(4040); 

io.on('connection', function (socket) { 
    socket.on('joinRoom', function(data){ 
     socket.join(data.room); 
    }); 

    // Other codes here... 
}); 

에 의해 시작 :

node /path/to/messenger.js 

감사합니다.

+0

socket.io에서 처리하지 않는 리소스를 요청할 때 어떤 결과가 발생합니까? –

+0

그냥 nginx 시간 초과 때까지 끊지. 404를 처리하기 위해 expressJS를 사용할 수 있다는 것을 알았습니다. http://socket.io/get-started/chat/ 먼저 시도해보고이를 업데이트하겠습니다. – Lysender

답변

0

대신 express + socket.io를 사용하여 사용자 정의 홈페이지를 제공하고 nonsocket.io URL에 404 오류를 발생시킵니다. 여기에서 아이디어를 얻었다 : http://socket.io/get-started/chat/

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

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

io.on('connection', function (socket) { 
    socket.on('joinRoom', function(data){ 
     socket.join(data.room); 
    }); 

    // Other codes 
}); 

http.listen(4040, function() { 
    console.log('Listening on *:4040'); 
}); 

을 그래서 우리는 같은 액세스하는 경우 : /favicon.ico 또는/foo는/바, 그것은 간단한 404 오류가 발생합니다.

관련 문제