How to work with websockets

Asked

Viewed 1,907 times

16

I’m trying to use Websockets in PHP and Javascript and I’m confused, it uses the protocol Ws:// and wss:// and the server (hostgator) does not have these protocols enabled (I think!) and I can’t get the connection, does anyone know if I need to enable these protocols on the server or how to make the connection ?

I tried to connect to another server I found in examples and got...
Follow the code (with server working):

<script>

var connection = new WebSocket('ws://echo.websocket.org/')

connection.onopen = function(e) {
  alert("Connected");
  console.log("Connected");
};

connection.onclose = function(e) {
  alert("Connection closed");
  console.log("Connection closed");
};

</script>


EDIT: Audio streaming is possible using websockets ?

  • Strange, I tested here on my browser console and it worked normal

  • This example is with another server, my server doesn’t work, but I don’t have any file on the server side to receive the data, nor do I find any file examples to receive the data... I researched a lot but I didn’t really understand how to work with websockets...

  • 3

    Here’s a full tutorial http://www.html5rocks.com/pt/tutorials/websockets/basics/

2 answers

2

0

The protocol ws:// and wss:// does not need to be installed or activated. This protocol is used by the browser to help identify the type of request and know how to better handle the header or body information during sending or returning the request.

This protocol is new and is available in browsers that support connection to websocket. It is not mandatory to use this protocol for connection, however it will make life much easier.

However, it is common to run the server script on a specific port, as 80 is already in use by Apache.

So your address may be Ws://localhost:8080/ for example.

The audio or video stream is nothing more than a transmission of small parts of files between the client to server and vice versa. So just create a websocket that receives and transmits the received data to customers.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.