node.js - Add to my chat nodejs express another my function -


i have basic chat working nodejs, express, , socket.io. i'm trying add own simple javascript function index.html. when send message other connected users, function loadrect() draw square on web page. want same square displayed on other users screens message. message has random coordinates x, y, , random color (massege, x, y, c). me please. (sorry english.)

index.html

<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 x;     var y;     var c;      function loadrect() {         var e = document.createelement("div");         e.classname = "rect";         document.body.appendchild(e);                 e.appendchild(document.createtextnode("square"));         e = e.style;         e.width = e.height='100px';         e.position = "absolute";         x = math.floor(math.random()*400+200)+'px';         y = math.floor(math.random()*400)+'px';         c = '#'+(math.random()*0xffffff<<0).tostring(16);         e.left = x;         e.top = y;         e.background = c;        }      var socket = io.connect('http://localhost:8181');      socket.on('connect', function(){         socket.emit('adduser', prompt("what's name?", "guest"));     });        socket.on('updatechat', function (username, data) {          $('#conversation').append('<b>' + username + ':</b> '+ data + '</br>');     });       socket.on('updateusers', function(data) {         $('#users').empty();         $.each(data, function(key, value) {             $('#users').append('<div>' + key + '</div>');         });     });      $(function(){         $('#datasend').click( function() {             var message = $('#data').val();                 $('#data').val('');                 //loadrect();                 loadrect();                 socket.emit('sendchat', message, x, y, c);     });      $('#data').keypress(function(e) {             if(e.which == 13) {                 $(this).blur();                 $('#datasend').focus().click();             }         });     });   </script>    <b>users</b>   <div id="users"></div>   <div id="conversation"></div>   <input id="data" />   <input type="button" id="datasend" value="send" /> 

app.js

var express = require('express')   , app = express()   , http = require('http')   , server = http.createserver(app)   , io = require('socket.io').listen(server); server.listen(8181);  app.get('/', function (req, res) {   res.sendfile(__dirname + '/index.html'); });   var usernames = {};  io.sockets.on('connection', function (socket) {    socket.on('sendchat', function (data, x, y, c) {     io.sockets.emit('updatechat', socket.username, data + '  [x=' + x + '; y=' + y + '; c=' + c +']');   });     socket.on('adduser', function(username){     socket.username = username;     usernames[username] = username;     socket.emit('updatechat', 'server', ' connected');     socket.broadcast.emit('updatechat', 'server', username + ', hello');     io.sockets.emit('updateusers', usernames);   });     socket.on('disconnect', function(){     delete usernames[socket.username];     io.sockets.emit('updateusers', usernames);     socket.broadcast.emit('updatechat', 'server', socket.username + ', bye');   }); }); 


Comments

Popular posts from this blog

php - Magento - Deleted Base url key -

javascript - Tooltipster plugin not firing jquery function when button or any click even occur -

java - WrongTypeOfReturnValue exception thrown when unit testing using mockito -