1. sayfa
var app = require('express')();var http = require('http').Server(app);var io = require('socket.io')(http);app.get('/', function(req, res){ res.sendFile(__dirname + '/index.html');});io.on('connection', function(socket){ socket.id="lala" socket.on('chat message', function(msg){ console.log('message: ' + msg); io.emit('chat message', socket.id); io.to("lala").emit('chat message', 'for your eyes only');//çalışmıyor socket.broadcast.to("lala").emit('chat message', 'for your eyes only');//çalışmıyor,zaten broadcast olduğu için kendine göndermemesi gerekiyor socket.to("lala").emit('chat message', 'for your eyes only');//çalışmıyor });});http.listen(3000, function(){ console.log('listening on *:3000');});
$(function () { var socket = io(); $('form').submit(function(){ socket.emit('chat message', document.getElementById("m").value); document.getElementById("m").value = ""; return false; }); socket.on('chat message', function(msg){ $('#messages').append($('<li>').text(msg)); }); });
quote:Orijinalden alıntı: Delifisek.Tux Öyle form post eder gibi yaparsan olmaz tabi. websocket server sunucuda senin tanımladığın bir portta çalışır. Sen ona javascript websocket ara yüzü ile bağlanmaya çalışırsın. https://socket.io/get-started/chat/ buradan devam et.
server kodları
html içindeki js kodları
serverda //çalışmıyor şeklinde belirttim. istediğim şey socket.id sini bildiğim birine bir mesaj göndermek