PNews jun 2015 | Page 18
Tutorials Point, Simply Easy Learning
Following are the events associated with WebSocket object. Assuming we created Socket object
as mentioned above:
Event
open
Event Handler
Socket.onopen
Description
This event occurs when socket connection is established.
message Socket.onmessage
This event occurs when client receives data from server.
error
Socket.onerror
This event occurs when there is any error in communication.
close
Socket.onclose
This event occurs when connection is closed.
WebSocket Methods:
Following are the methods associated with WebSocket object. Assuming we created Socket
object as mentioned above:
Method
Description
Socket.send()
The send(data) method transmits data using the connection.
Socket.close()
The close() method would be used to terminate any existing
connection.
WebSocket Example:
A WebSocket is a standard bidirectional TCP socket between the client and the server. The
socket starts out as a HTTP connection and then "Upgrades" to a TCP socket after a HTTP
handshake. After the handshake, either side can send data.
Client Side HTML & JavaScript Code:
At the time of writing this tutorial, there are only few web browsers supporting WebSocket()
interface. You can try following example with latest version of Chrome, Mozilla, Opera and
Safari.