Socket.IO and Complex JSON with Node.js, jQuery

≯℡__Kan透↙ 提交于 2019-12-25 00:52:37

问题


I'm having a ton of trouble with this. I'm trying to make it so that when I move a square-div, the position of that div is relayed to another page via Socket.IO, so that the div moves in realtime. Only problem is that I don't have a clue how to do this! The documentation over at http://socket.io only confused me.

My jQuery code:

$(document).ready(function() {
    $("#mydiv").draggable().mousemove(function(){
        var coord = $(this).position();
        $("#left").val(coord.left);
        $("#top").val(coord.top);
    });
});

html:

X: <input type="text" value="50" id="left"/>
Y: <input type="text" value="50" id="top"/>

<div id="element"></div>

​I don't even know where to start with socket.io, please help!

Thank you very much!


回答1:


Socket.IO consists of two parts:

  1. A server program that can send and receive data from clients
  2. A client script that can connect to the Socket.IO server and send and receive data

So, first of all, you need a server. Here's one that listens on port 8080 and when a client connects, it waits for an receive_position event from it, and when it gets one, broadcasts that position to all the rest of the connected clients via an update_position event.

There is also some code to serve an index.htm file when the root URL (/) is visited. Most of this code is from the Socket.IO "How to Use" page; if the non-Socket.IO code doesn't make any sense, you may want to brush up on your Node.js fundamentals.

var app = require('http').createServer(handler)
  , io = require('socket.io').listen(app)
  , fs = require('fs')

app.listen(8080);

function handler (req, res) {
  fs.readFile(__dirname + '/index.html',
  function (err, data) {
    if (err) {
      res.writeHead(500);
      return res.end('Error loading index.html');
    }

    res.writeHead(200);
    res.end(data);
  });
}

var lastPosition = { x: 0, y: 0 }; // whatever default data

io.sockets.on('connection', function (socket) {
  socket.emit('update_position', lastPosition);
  socket.on('receive_position', function (data) {
     lastPosition = data;
     socket.broadcast.emit('update_position', data); // send `data` to all other clients
  });
});

Now that your server is set up, you need some client-side scripting to send and receive the positions of the div. Put this code in your index.htm file.

<script src="/socket.io/socket.io.js"></script>
<script>
  $(document).ready(function() {
    var socket = io.connect();
    socket.on('update_position', function (data) {
      var x = data.x;
      var y = data.y;
      // jquery code to move div here
    });

    $("#mydiv").draggable().mousemove(function(){
      var coord = $(this).position();
      $("#left").val(coord.left);
      $("#top").val(coord.top);
      socket.emit('receive_position', { x: coord.left, y: coord.top });
    });
  });
</script>

This code sends a receive_position event when the div is dragged; the server gets this event, and sends an update_position event with the same x and y values to all other clients; when they receive this data, they update the div.

Hope this helps get you started; let me know in the comments if you have any questions.



来源:https://stackoverflow.com/questions/9865167/socket-io-and-complex-json-with-node-js-jquery

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!