Don't convert white spaces into "&nbsp"s.
Remove trailing spaces.
Refactor the handler for ws.onmessage.