WriteJSON writes to the underlying WebSocket connection given object, marshaled using json.Marshal().
(object interface{})
| 478 | // WriteJSON writes to the underlying WebSocket connection given object, |
| 479 | // marshaled using json.Marshal(). |
| 480 | func (ws *Websocket) WriteJSON(object interface{}) *Websocket { |
| 481 | opChain := ws.chain.enter("WriteJSON()") |
| 482 | defer opChain.leave() |
| 483 | |
| 484 | if ws.checkUnusable(opChain, "WriteJSON()") { |
| 485 | return ws |
| 486 | } |
| 487 | |
| 488 | b, err := json.Marshal(object) |
| 489 | |
| 490 | if err != nil { |
| 491 | opChain.fail(AssertionFailure{ |
| 492 | Type: AssertValid, |
| 493 | Actual: &AssertionValue{object}, |
| 494 | Errors: []error{ |
| 495 | errors.New("invalid json object"), |
| 496 | err, |
| 497 | }, |
| 498 | }) |
| 499 | return ws |
| 500 | } |
| 501 | |
| 502 | ws.writeMessage(opChain, websocket.TextMessage, b) |
| 503 | |
| 504 | return ws |
| 505 | } |
| 506 | |
| 507 | func (ws *Websocket) checkUnusable(opChain *chain, where string) bool { |
| 508 | switch { |