这就是我在浏览器控制台中得到的:
WS-PARSER: received {"eventPhase":2,"origin":"","bubbles":false,"defaultPrevented":false,"srcElement":{"binaryType":"blob","extensions":"","url":"ws://localhost:9000/map-socket/a@a.com","bufferedAmount":0,"readyState":1,"onerror":null,"onopen":null,"onclose":null,"protocol":"","URL":"ws://localhost:9000/map-socket/a@a.com"},"type":"message","returnValue":true,"target":{"binaryType":"blob","extensions":"","url":"ws://localhost:9000/map-socket/a@a.com","bufferedAmount":0,"readyState":1,"onerror":null,"onopen":null,"onclose":null,"protocol":"","URL":"ws://localhost:9000/map-socket/a@a.com"},"source":null,"cancelable":false,"currentTarget":{"binaryType":"blob","extensions":"","url":"ws://localhost:9000/map-socket/a@a.com","bufferedAmount":0,"readyState":1,"onerror":null,"onopen":null,"onclose":null,"protocol":"","URL":"ws://localhost:9000/map-socket/a@a.com"},"ports":[],"timeStamp":1341430631884,"lastEventId":"","cancelBubble":false,"data":"{\"stars\":[{\"x\":\"0.0\",\"y\":\"0.0\",\"own\":\"-1\",\"id\":\"0\",\"units\":\"0\"},{\"x\":\"0.0\",\"y\":\"0.0\",\"own\":\"-1\",\"id\":\"1\",\"units\":\"0\"},{\"x\":\"0.0\",\"y\":\"0.0\",\"own\":\"0\",\"id\":\"2\",\"units\":\"0\"}]}"}
我有两个问题:
1)为什么消息的第一部分(自动)有那么多标题重复?可以吗,我应该这样保留还是可以以某种方式减少样板的数量?
2)为什么消息的第二部分(我的有效负载)中的所有引号都被转义了?
这是制定 JSON 的代码:
val stars = strs.getAll.map(_.asJsValue).toSeq
Json.toJson(
Map(
"stars" -> stars
)
)
asJsValue
方法:
def asJsValue = {
Json.toJson(
Map(
"id" -> id.toString,
"x" -> x.toString,
"y" -> y.toString,
"units" -> units.toString,
"own" -> getOwnerID.toString
)
)
}
在客户端:
websocket.onmessage = receivedEvent
receiveEvent = (event) ->
console.log("WS-PARSER: received " + JSON.stringify(event))