1. How to run Java scripts in a web browser

In the Chrome browser, Ctrl + Shift + I to access developer tools. You can run JavaScript directly from the Console tab.

 

2. Java script to open a web connection

1) Source

<!DOCTYPE html>
<meta charset="utf-8" />
<title>WebSocket Test</title>
<script language="javascript" type="text/javascript">

    var wsUri = "ws://echo.websocket.org/";
var output;
var loopCount = 0;

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("Hello "+loopCount.toString());

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

  function onMessage(evt)
  {
    writeToScreen('<span style="color: blue;">RESPONSE: ' + evt.data+'</span>');
	if (loopCount < 10)
	{
		doSend("Hello "+loopCount.toString());
		loopCount = loopCount + 1;
	}
	else
	{
	    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>

2) Run

Live Server Plugin in Visual Studio Code opened a virtual server to create a hands-on environment.

 

3. Analyze How the WebSocket connection is built by HTTP

1) Packet Analyze

2) HTTP

(1) HTTP request message

(2) HTTP response message

3) And change protocol HTTP to Web socket

 

 

4. Send data using the WebSocket connection from a browser to a server and check data is exchanged; where is the data included ?

(1) client to server

(2) Server to client

'Study > Network' 카테고리의 다른 글

Http(https) Filter  (0) 2019.11.22
DNS Query and HTTP Connection  (0) 2019.10.14
TCP / UDP Socket Programming  (0) 2019.09.25

+ Recent posts