Refactor client onmessage