通过Websockets发送多点触摸事件

Sending multi-touch events over Websockets

本文关键字:触摸 事件 多点 Websockets 通过      更新时间:2023-09-26

我试图将触摸事件对象编码为JSON通过websockets发送,出现错误说:

Uncaught TypeError: conversion circular structure to JSON

当对象的属性直接是对象本身时,会发生错误。那么,通过websockets发送多点触摸事件的方式是什么呢?

document.addEventListener('touchmove', function(event) {
  console.log(event);
  event = JSON.stringify(event.touches); // Error!
  connection.send(event);
}, false);
http://jsfiddle.net/PAVtk/

您正在尝试字符串化多个Touch对象。这些对象包含对开始触摸的元素的引用。htmlelement通常有循环引用。

您应该在将触摸对象发送到服务器之前删除该元素。你可以先克隆它。

delete touchObject["target"];