HTML and CSS Reference
In-Depth Information
The data from the client is sent to the server when you click the Send button. The Send button's click
event handler looks like this:
$(“#btnSend”).clickhow(function () {
if (socket.readyState == WebSocket.OPEN) {
socket.send($(“#txtMsg”).val());
}
else {
$(“#divHistory”).append('<h3>The underlying connection is closed.</h3>');
}
});
The click event handler checks the readyState property of the WebSocket object. If it's OPEN , the click
event handler calls the WebSocket instance's send() method. The text entered in the text box is passed as a
parameter to send() . Although not used in this application, you can close the underlying connection by
calling the WebSocket object's close() method:
socket.close();
That's it! You can now run the web form and test the Echo server by sending messages to the server.
n Note The Windows Communication Foundation (WCF) in .NET Framework 4.5 also supports Web Sockets. Two
new bindings have been added to WCF for this purpose: NetHttpBinding and NetHttpsBinding . Detailed
discussion of how WCF supports Web Sockets is beyond the scope of this topic. Refer to the MSDN documentation
for more details.
Summary
Web applications often need to communicate with the server. Especially in Ajax-driven applications, you
need to communicate with the server without a full page post-back. HTML5 offers several ways to achieve
client-server communication. The postMessage API allows you to perform cross-document messaging, by
means of which you can send data to a web page having a different origin. XMLHttpRequest is the basis of
Ajax-based communication between the client and the server. XMLHttpRequest Level 2 lets you monitor the
progress of the data-upload and -download operations. It also allows cross-origin communication through
CORS.
Typical web communication uses the request-response model. You can use server-sent events if you
need one-way communication from server to client. This way, the server can notify the client about
happenings on the server. Web Sockets offer full-duplex communication between client and server. IIS 8.0,
which ships with Windows 8, provides server-side support for the WebSocket protocol. The .NET
framework also offers a set of classes to work with Web Sockets.
The next chapter peeks into yet another feature that can be useful in certain types of web applications:
geolocation. Using geolocation, you can determine the geographical location of a user and change how
your web application responds accordingly.
 
 
Search WWH ::




Custom Search