4

I am new to computer science.I am learning nodejs. I see a lot of examples where developer uses url like this

ws://echo.websocket.org
ws://localhost:8080/echo
ws://sandbox.kaazing.net/echo

What does this mean? Why do we need this when we can make chat apps or maybe any real time app? How can I create my own ? I tried to create my own but I don't know what I have to write. I just write this code only

var ws=require('ws');
var http=require('http');
http.createServer(function(req, res){

// here i dont know what i write
}).listen(8080);

I tried to goole how can I create/build url like those... but I could not find any good tutorial.

..............................................

<!DOCTYPE html>
<meta charset="utf-8" />
<title>WebSocket Test</title>
<script language="javascript" type="text/javascript">
    var wsUri = "ws://echo.websocket.org/";
    var output;

    function init() {
        output = document.getElementById("output");
        testWebSocket();
    }

    function testWebSocket() {
        websocket = new WebSocket(wsUri);
        websocket.onopen = function(evt) {
            onOpen(evt)
        };
        websocket.onclose = function(evt) {
            onClose(evt)
        };
        websocket.onmessage = function(evt) {
            onMessage(evt)
        };
        websocket.onerror = function(evt) {
            onError(evt)
        };
    }

    function onOpen(evt) {
        writeToScreen("CONNECTED");
        doSend("WebSocket rocks");
    }

    function onClose(evt) {
        writeToScreen("DISCONNECTED");
    }

    function onMessage(evt) {
        writeToScreen('<span style="color: blue;">RESPONSE: ' + evt.data + '</span>');
        websocket.close();
    }

    function onError(evt) {
        writeToScreen('<span style="color: red;">ERROR:</span> ' + evt.data);
    }

    function doSend(message) {
        writeToScreen("SENT: " + message);
        websocket.send(message);
    }

    function writeToScreen(message) {
        var pre = document.createElement("p");
        pre.style.wordWrap = "break-word";
        pre.innerHTML = message;
        output.appendChild(pre);
    }
    window.addEventListener("load", init, false);
</script>
<h2>WebSocket Test</h2>
<div id="output"></div>

I snippet this code from https://www.websocket.org/echo.html.

kiranvj
  • 32,342
  • 7
  • 71
  • 76
  • 2
    i tried my best to explain my question please do not downgrade vote me :( –  Sep 04 '15 at 16:35

1 Answers1

0

What you're referring to are Protocol Handlers.

ws is the websocket protocol (wikipedia). In order to create a custom protocol, a user would have to install an application on their client. In windows, these are saved in the registry. See ShotgunSoftware Article.

Defining a protocol on a server application created in NodeJS is not feasible. You will only be able to define it on the server at best.

I hope this cleared up any confusion, please let me know if I can provide clarification.

CollinD
  • 7,304
  • 2
  • 22
  • 45
  • i am not understand yet, what i have to do for creating custom url ?hmm what i do to create an url like this ws://localhost:8000 or ws://localhost/echo.. or wss –  Sep 04 '15 at 17:02
  • Are you trying to develop a server application that is capable of listening for the websocket protocol? – CollinD Sep 04 '15 at 17:05
  • 1
    I'd head over to http://stackoverflow.com/questions/16392260/which-websocket-library-to-use-with-node-js – CollinD Sep 04 '15 at 17:27
  • i have already seen that but i dont know lol how to use.. i watch some vid on youtube and they was using custom url/protocol ws://localhost;port/echo –  Sep 04 '15 at 17:47