Demo entry 6787160

1

   

Submitted by anonymous on Apr 10, 2019 at 08:25
Language: HTML. Code size: 2.1 kB.

<!DOCTYPE HTML>
<html>
<head>
  <title>Example WebSocket</title>
</head>
<body>

<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>
<script type="text/javascript">

if ('WebSocket' in window) {
	var isconnected = 0;
	var lksstatus = 0;
	
	var ws = new WebSocket('ws://127.0.0.1:33210/chk'); // 서버 설치 여부 확인 프로토콜
	
	//var test = '{name: “James Devilson”, message: “Hello World!”}';


	ws.onopen = function () {
		$('#status').text('connected');

		ws.send(JSON.stringify({
			"protocol" : "CheckServer",
			"message": "Server Hello"
		})); // 확인 메시지 전송
	};
  
	// 메세지를 받았을때
	ws.onmessage = function (evt) {
		
		var jsonObj = JSON.parse(evt.data);
		
		if(jsonObj.protocol == 'CheckServer') { // 응답 메시지 확인
 
			$('#messages').append($('<li>').text('Received vtb protocol: ' + jsonObj.protocol + ', message: ' + jsonObj.message));
			
			if(jsonObj.message == 'Client Hello') {
				isconnected = 1;

				ws.send(JSON.stringify({ 
					"protocol" : "CheckLKS",
					"message": "LKS Check"
				})); // 확인 메시지 전송
			}
		}
		if(isconnected == 1 ) { // 응답 메시지 확인
			
			if(jsonObj.protocol == 'CheckLKS') { 
				
				$('#messages').append($('<li>').text('Received vtb protocol: ' + jsonObj.protocol + ', LKS is: ' + jsonObj.isInserted + ', LKS ID: ' + jsonObj.LKS_ID));
				
				if(jsonObj.isInserted == 1) { 
					lksstatus = 1;
					
					var ws2 = new WebSocket('ws://127.0.0.1:33210/cam'); // 서버 설치 여부 확인 프로토콜
					
					ws2.onopen = function () {
						ws2.send("caminfo");
					};
					
					ws2.onmessage = function (evt) {
		
						$('#messages').append($('<li>').text('Received cam protocol: ' + evt.data));
				
					}
				}
			}
		}
		
		
			
	};
	
	ws.onclose = function () {
		if(isconnected == 0)
			$('#status').text('sever is not running');
		else
			$('#status').text('connection is closed');
	};
}
else
  $('#status').text('WebSocket not supported.');
</script>

Status: <span id="status"></span><br /><br />
Messages: <ul id="messages"></ul>
</body>
</html>

This snippet took 0.01 seconds to highlight.

Back to the Entry List or Home.

Delete this entry (admin only).