Game Development Reference
In-Depth Information
con.addEventListener("message", doOnMessage);
con.addEventListener("close", doOnClose);
function doOnOpen(event) {
var msg = document.createElement("p");
msg.textContent = "Socket connected to " +
event.srcElement.URL;
document.body.appendChild(msg);
}
function doOnError(event) {
var msg = document.createElement("p");
msg.textContent = "Error: " + event;
document.body.appendChild(msg);
}
function doOnMessage(event) {
var response = JSON.parse(event.data);
var msg = document.createElement("p");
msg.textContent = "Message received: " +
response.message;
document.body.appendChild(msg);
}
function doOnClose(event) {
var msg = document.createElement("p");
msg.textContent = "Socket connection closed
at " +
event.timeStamp;
document.body.appendChild(msg);
}
// Step 3: Send a message to the server
con.send("Hello!");
As you can see from the preceding code snippet, there aren't too many differences
between the web socket interface and the web worker interface. Most notably, per-
haps, is the actual interface through which we can post a message to the server.
Search WWH ::




Custom Search