简体   繁体   English

通过网络套接字发送在Play框架中创建的JSON

[英]Sending a JSON created in Play framework over a websocket

That is what I get in browser console: 那就是我在浏览器控制台中得到的:

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\"}]}"}

I have 2 questions: 我有两个问题:

1) Why does the first part of the message (automatic) has that many header repetitions? 1)为什么消息的第一部分(自动)具有如此多的标题重复项? Is this ok, should I leave it like that or can I somehow reduce the amount of the boilerplate? 这样可以吗,我应该那样做还是可以以某种方式减少样板的数量?

2) Why are all the quotes in the second part of the message (my payload) escaped? 2)为什么消息第二部分(我的有效负载)中的所有引号都转义了?

Here is the code that formulates the JSON: 这是公式化JSON的代码:

val stars = strs.getAll.map(_.asJsValue).toSeq
Json.toJson(
    Map(
        "stars" -> stars
        )
)

the asJsValue method: asJsValue方法:

  def asJsValue = {
    Json.toJson(
        Map(
            "id" -> id.toString,
            "x" -> x.toString,
            "y" -> y.toString,
            "units" -> units.toString,
            "own" -> getOwnerID.toString
        )
    )
  }

and on the client side: 在客户端:

websocket.onmessage = receivedEvent

receiveEvent = (event) ->
  console.log("WS-PARSER: received " + JSON.stringify(event))
  1. Most of that data is added on the client-side, only the data value is sent over the wire 大部分数据添加在客户端,只有data值通过网络发送
  2. JSON requires double quotes, that's why they are escaped JSON需要双引号,这就是它们被转义的原因

(disclaimer: not a play user) (免责声明:不是游戏用户)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM