Anyone got a good WebSocket tutorial?
-
I read up a bit on MDN and websockets.org, and wrote a simple, 50-line HTML page to start experimenting with them, but when I load the page I get an error like this, in IE11:
WebSocket Error: Incorrect HTTP response. Status code 200, OK
Chrome gives me a similar error. Obviously I'm doing something wrong, but googling that error doesn't return anything I found useful. The page on the other end is just a trivial classic ASP that returns one word.
-
My sample page: this isn't intended to be clever or anything; it's just a really basic page with the beginnings of some test code:
[code]<!DOCTYPE html>
<html lang="en" xmlns="http://www.w3.org/1999/xhtml"> <head> <style> body { font : 12pt Segoe UI} input { font : 12pt Segoe UI} </style> <meta charset="utf-8" /> <title></title> <script type="text/javascript"> var ws; function clicker() { alert("clicked"); // ws.send("test"); ws.close(); } function loader() { document.getElementById("b").onclick = clicker; document.getElementById("r").innerHTML = "start"; console.log("one"); ws = new WebSocket("ws://localhost/wstest.asp"); document.getElementById("r").innerHTML += "
Opening";ws.onmessage = function (event) { document.getElementById("r").innerHTML += event.data; console.log(event.data); } ws.onopen = function () { document.getElementById("r").innerHTML = "opened"; ws.send("test"); } console.log("four"); } </script>
blah
<input type="button" id="b" value="go!"/> [/code]
-
I think you can use Chrome now to monitor the communication. I'd start with dumping the headers.
I'm guessing there's a problem server-side - you don't want 200 with WS. Can you post the ASP code?
-
My first thought is, why not use signalr?
But OK, assuming you just want to learn the basics, I would like to see ASP code, so I can set up a project and play a bit with this.
-
i've never done websockets on the C# side but from everything i've read @cartman82's onto a winner there with signalr
Apparently signalr is to C# as socket.io is to nodejs, and i swear by (and occasionally at) socket.io
-
Also:
wstest.asp
Is that classic-classic ASP? Does that even support websockets? Did you actually set anything up server-side to enable websockets, or are you just doing
<%Response.Write("Hello world!");%>
?
-
I don't think anything prevents you from making a WebSockets server in Classic ASP, but it's kind of like making a supercharger for your Audi using whale-bone. Why would you do such a thing?
But yeah, I was going to mention the error looks like it's just due to talking to a normal non-WebSockets URL, which returns a normal HTTP response and doesn't keep the stream open like the browser expects.
-
But OK, assuming you just want to learn the basics, I would like to see ASP code, so I can set up a project and play a bit with this.
A meaningful response from the ASP was going to wait until I had the client side looking like it worked. All I had was
<% response.write "hi" %>
so that I wouldn't get a 404.I hadn't gotten as far as the server, and the pages I've seen so far didn't mention the response code. Clearly it's not like AJAX though.
-
My first thought is, why not use signalr?
Well, because I hadn't heard of it until just now. I decided this morning I wanted to start investigating WebSockets, mostly on a whim.
-
Could be--as I said just now I haven't seen any server-side stuff yet. I literally (so far) know nothing about WS so far that you couldn't glean from Ars Technica and 5 minutes on websockets.org (or MDN).
-
Do you have much experience with ordinary sockets either?
My understanding is that the server has to respond to the socket creation attempt with a socket, and then sit there holding it open and pumping information down it periodically. It's not a one-and-done like AJAX would be.
-
-
That'll make things easier then. A lot of the devs I work with had never heard of sockets before socket.io >.>
-
A lot of the devs I work with had never heard of sockets before socket.io
Those are, like, the things you stick a fork in and then you get that funny tickle for a split second and then you go flying and then you're in pain?
-
Oh come on, you don't always get sent flying!
-
Uses ASP.NET but also has a WCF and generic server-side articles.
-
Oh come on, you don't always get sent flying!
Not with your weaksauce American voltages, no
-
-
We're also big into tea, so we need an electrical system that can power an electric kettle on demand
-
big into tea [...] electric kettle
If you're going to drink boiled leaves like an animal you should do it the old-fashioned way: on an open fire.
-
I thought Europeans were big into safety.
We are. That's why we have higher voltages (since that lets us lower currents, reducing problems from overheating in the wires). As long as your wiring insulation is sufficient (protip: it probably is already) and you're earthing things as necessary, you're fine.
The British style of electrical socket is grossly overengineered, even more so than our habit of using ring mains in domestic properties.