それがブラウザコンソールで得られるものです:
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\"}]}"}
2 つの質問があります。
1) メッセージの最初の部分 (自動) のヘッダーの繰り返しが多いのはなぜですか? これでいいですか、そのままにしておくべきですか、それともボイラープレートの量を減らすことができますか?
2) メッセージの 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))