0
私はnode.jsとsocket.ioで初めてです。公式のsocket.ioウェブサイトの 'get started'コードに続いて、私は簡単なチャットを書いています。その後、私はメッセンジャーのように別の部屋を作りたいと思っています。しかし、公式ドキュメントに頼って、私は自分自身としてこれを行うことができない...。ここ は私サーバコードです:socket.ioの部屋。メッセージの送信方法
io.on('connection', function(socket) {
var room_name = socket.request.headers.referer; // link of the page, where user connected to socket
// connecting to room
socket.join(room_name, function(){
//trying to send messages to the current room
io.to(room_name, function() {
socket.on('chat message', function (msg) {
io.emit('chat message', msg);
});
});
});
});
そして、私のクライアント側のコード:
$(function() {
var socket = io();
//when we submit our form
$('form').submit(function(){
//we are sending data to socket.io on server
socket.emit('chat message', $('#m').val());
$('#m').val('');
return false;
});
//And then we are creating message in our html code
socket.on('chat message', function(msg){
$('#messages').append($('<li>').text(msg));
});
});
なぜこれをコードは機能していませんか?メッセージを別の部屋に送信するにはどうすればよいですか?