HTML and CSS Reference
In-Depth Information
wsConnection.onmessage = function (msg) {
//write message
NewLine();
chatBox.value = chatBox.value + "Them: " + msg.data;
}
//helper functions.
function NewLine()
{
chatBox.textContent = chatBox.textContent + '\r\n';
}
}
</script>
<body>
<div align="center">
<div>
70-480 Study Group Chat Forum
</div>
<div>
<textarea id="chatWindow" style="height: 500px; width: 300px">
</textarea>
</div>
<div>
<input type="text" id="msgSendText" style="width: 300px"/>
</div>
<div>
<button id="Disconnect">Disconnect</button>
<button id="Connect">Connect</button>
<button id="Send">Send</button>
</div>
</div>
</body>
The primary object that you will work with is the
WebSocket
object, which connects to the
socket when its constructor is invoked. In Listing 2.1, a variable is declared but not instanti-
ated until a user invokes the connect button. When the user clicks the button, the
WebSocket
is instantiated with the appropriate connection information:
wsConnection= new WebSocket('ws://studygroup.70480.com', ['soap', 'xmpp']);
The
WebSocket
constructor accepts two parameters:
■
The URL of the server-side socket to connect to, which is always prefixed with
ws
or
wss
for secure
WebSocket
connections
■
An optional list of subprotocols
When the
WebSocket
constructor is called, the WebSocket API establishes a connection
to the server. One of two things can happen at this stage. The
WebSocket
will successfully
connect to the server or the connection will fail, resulting in an error. Both cases should be
Search WWH ::
Custom Search