名前空間を使用して socket.io チャット アプリのバージョンを区別していますが、「ブラウザに表示されるエラーを取得できません」という問題が発生しています。
基本的な socket.io チュートリアルで作成したチャット アプリを継続的に更新する予定で、いつでも任意のバージョンを起動できるようにしたいと考えています。名前空間を使用してこれを行います。アプリのバージョン 0.0.1 にアクセスするために myserverlocation/v0.0.1 の場所でブラウザーでアプリを起動すると、'/v0.0.1' を取得できないというエラーが表示されます。
これは私のサーバーコードです:
var app = require('express')(),
server = require('http').Server(app),
io = require('socket.io').listen(server),
chat = io.of('/v0.0.1');
server.listen(80);
// routing
app.get('/', function (req, res) {
res.sendfile(__dirname + '/index.html');
});
// usernames which are currently connected to the chat
var usernames = {};
chat.on('connection', function (socket) {
// when the client emits 'sendchat', this listens and executes
socket.on('sendchat', function (data) {
// we tell the client to execute 'updatechat' with 2 parameters
io.sockets.emit('updatechat', socket.username, data);
});
// when the client emits 'adduser', this listens and executes
socket.on('adduser', function(username) {
// we store the username in the socket session for this client
socket.username = username;
// add the client's username to the global list
usernames[username] = username;
// echo to client they've connected
socket.emit('updatechat', 'SERVER', 'you have connected');
// echo globally (all clients) that a person has connected
socket.broadcast.emit('updatechat', 'SERVER', username + ' has connected');
// update the list of users in chat, client-side
io.sockets.emit('updateusers', usernames);
});
// when the user disconnects.. perform this
socket.on('disconnect', function() {
// remove the username from global usernames list
delete usernames[socket.username];
// update list of users in chat, client-side
io.sockets.emit('updateusers', usernames);
// echo globally that this client has left
socket.broadcast.emit('updatechat', 'SERVER', socket.username + ' has disconnected');
});
});
そして、これは私のクライアントコードです:
<script src="/socket.io/socket.io.js"></script>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.6.4/jquery.min.js"></script>
<script>
var socket = io.connect('myserverlocation');
var chat = socket.of('/v0.0.1');
// on connection to server, ask for user's name with an anonymous callback
chat.on('connect', function(){
// call the server-side function 'adduser' and send one parameter (value of prompt)
chat.emit('adduser', prompt("What's your name?"));
});
// listener, whenever the server emits 'updatechat', this updates the chat body
chat.on('updatechat', function(username, data) {
$('#conversation').append('<b>' + username + ':</b> ' + data + '<br>');
});
// listener, whenever the server emits 'updateusers', this updates the username list
chat.on('updateusers', function(data) {
$('#users').empty();
$.each(data, function(key, value) {
$('#users').append('<div>' + key + '</div>');
});
});
// on load of page
$(function(){
// when the client clicks SEND
$('#datasend').click( function() {
var message = $('#data').val();
$('#data').val('');
// tell server to execute 'sendchat' and send along one parameter
chat.emit('sendchat', message);
});
// when the client hits ENTER on their keyboard
$('#data').keypress(function(e) {
if(e.which == 13) {
$(this).blur();
$('#datasend').focus().click();
}
});
});
</script>
<div style="float:left;width:100px;border-right:1px solid black;height:300px;padding:10px;overflow:scroll-y;">
<b>USERS</b>
<div id="users"></div>
</div>
<div style="float:left;width:300px;height:250px;overflow:scroll-y;padding:10px;">
<div id="conversation"></div>
<input id="data" style="width:200px;" />
<input type="button" id="datasend" value="send" />
</div>
私のチャット アプリは、名前空間を使用しなくても正常に動作します (myserverlocation/. なぜこのエラーが発生し続けるのかわかりません。いくつかの調査の後、io.of() の使用法が間違っていると思いますが、問題を解決できないようです。問題がサーバー コードにあるのか、クライアント コードにあるのか、またはその両方にあるのかわかりません。
編集:さらに調査した結果、私の問題はコードの次のセグメントにあると思います(ただし、間違っている可能性があります):
app.get('/', function (req, res) {
res.sendfile(__dirname + '/index.html');
});
Edit2: 実際、問題は上記のコード セグメントにありました。res.sendfile() を使用して 1 つのファイルを送信するのではなく、/Chat ディレクトリ全体を静的コンテンツとして送信する必要がありました。stackoverflow で許可されたら、自分の質問に正式に回答します (自分の質問に回答するには 8 時間待たなければなりません)。