Monday, 15 September 2014

javascript - socket.io Connected to late -



javascript - socket.io Connected to late -

i have nodejs project

but socket.io connecting it's late.

its first opened, socket.io not connected. socket.io connected 10-15 seconds later.

my server js :

var io = require('socket.io').listen(8080); function getusernames(room) { var clients = io.sockets.clients(room); client=new array(); clients.foreach(function(_client) { client.push({username:_client.username,room:_client.room}) }); homecoming client; } io.sockets.on('connection', function(client){ client.on('login', function(username, room){ client.username = username; client.room = room; client.join(client.room); io.sockets.to(client.room).emit('updateuser', getusernames(client.room)); }); client.on('mesajgonder', function(data){ client.emit('mesajgitti', data) client.broadcast.emit('mesajgitti', data) }); client.on('disconnect', function(){ var eskiroom = client.room; client.leave(client.room); io.sockets.to(eskiroom).emit('updateuser', getusernames(eskiroom)); }); });

my client js :

var socket; var room = 'room8'; var username = 'username'; var socket = io.connect("http://localhost:8080"); $(".text_metin").append('<span><p>welcome to</p> <b>`'+room+'`</b> write now!</span>'); socket.on('connect', function(s){ socket.emit('login', username, room); socket.on("mesajgitti", function(data){ $(".text_metin").append("<li id='chat_metin'>" + data.mesaj + "</li>"); }); socket.on('updateuser', function(users){ $.each(users, function(i, user) { $("#user_liste").empty(); $("#user_liste").append('<li><img src="images/1111.png" alt="" width="20" height="20" /><a href="#" title="coming soon">'+user.username+'</a></li>'); }); }); }); $("#mesajgonder").click(function(){ var mesaj = $("#writechat").val(); if (!mesaj){ return; } socket.emit('mesajgonder',{ 'mesaj' : mesaj, }); $("#mesajinput").val(""); }); $(".tags li a").click(function(){ var demo = $(this).text(); $("#interested").val($("#interested").val()+','+demo); }); $("#mesajgonder").click(function(){ $("#writechat").val(''); });

i not utilize express. give thanks you

javascript node.js socket.io

No comments:

Post a Comment