LearnBoost / websocket.io

329 stars 59 forks source link

Client-side example needed #7

Closed jaxcore closed 12 years ago

jaxcore commented 12 years ago

There doesn't seem to be a cut-n-paste example of how to use "new WebSocket()" in a web browser against this codebase. What I am trying does not appear to work in the latest versions of Firefox and Chrome (protocol version 8), however Safari does work (using "drafts" protocol).

In protocol 8 an http upgrade event occurs, but server.on('connection') is not called, and the client doesn't receive an "onopen" event.

Here's what I'm using for the server:

var ws = require('websocket.io') , server = ws.listen(3000) server.on('connection', function (client) { console.log('connection'); client.send('client ping'); client.on('message', function (data) { console.log('message: '+data) }); client.on('close', function () { console.log('close') }); });

And here's my client-side HTML file:

<script> var WS = (typeof MozWebSocket=='function')? MozWebSocket : WebSocket; var websocket = new WS('ws://localhost:3000'); websocket.onopen = function () { console.log('open'); websocket.send('server ping'); }; websocket.onerror = function (error) { console.log('error '+error); }; websocket.onmessage = function (e) { console.log('message: '+e.data); }; websocket.onclose = function(data) { console.log('close'); }; </script>

emerleite commented 12 years ago

Works nice for me using safari. Sending a pull request.

jaxcore commented 12 years ago

Yes I also discovered it works in Safari, I've updated this issue with the exact code I'm trying.