WebSocket
WebSocket is a protocol providing full-duplex communication channels over a single TCP connection. The WebSocket protocol was standardized by the IETF as RFC 6455 in 2011, and the WebSocket API in Web IDL is being standardized by the W3C.
Categories
Proxy
- Websockify - WebSocket to TCP proxy/bridge (noVNC 에서 사용함)
- wstcp - WebSocket to TCP proxy written in Rust
- Nginx#NginX WebSocket Proxy
Example
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Native WebSocket Example</title>
</head>
<body>
<script>
// 웹소켓 전역 객체 생성
var ws = new WebSocket("ws://localhost:3000");
// 연결이 수립되면 서버에 메시지를 전송한다
ws.onopen = function(event) {
ws.send("Client message: Hi!");
}
// 서버로 부터 메시지를 수신한다
ws.onmessage = function(event) {
console.log("Server message: ", event.data);
}
// error event handler
ws.onerror = function(event) {
console.log("Server error message: ", event.data);
}
</script>
</body>
</html>
Libraries
- WebSocket++
- https://www.zaphoyd.com/websocketpp
- https://github.com/zaphoyd/websocketpp
- WebSocket++ is an open source (BSD license) header only C++ library that impliments RFC6455 The WebSocket Protocol. It allows integrating WebSocket client and server functionality into C++ programs. It uses interchangable network transport modules including one based on C++ iostreams and one based on Boost Asio.
Receiving and interpreting JSON objects
exampleSocket.onmessage = function(event) {
var f = document.getElementById("chatbox").contentDocument;
var text = "";
var msg = JSON.parse(event.data);
var time = new Date(msg.date);
var timeStr = time.toLocaleTimeString();
switch(msg.type) {
case "id":
clientID = msg.id;
setUsername();
break;
case "username":
text = "<b>User <em>" + msg.name + "</em> signed in at " + timeStr + "</b><br>";
break;
case "message":
text = "(" + timeStr + ") <b>" + msg.name + "</b>: " + msg.text + "<br>";
break;
case "rejectusername":
text = "<b>Your username has been set to <em>" + msg.name + "</em> because the name you chose is in use.</b><br>"
break;
case "userlist":
var ul = "";
for (i=0; i < msg.users.length; i++) {
ul += msg.users[i] + "<br>";
}
document.getElementById("userlistbox").innerHTML = ul;
break;
}
if (text.length) {
f.write(text);
document.getElementById("chatbox").contentWindow.scrollByPages(1);
}
};
See also
- HTML5
- Ajax
- Comet
- Polling
- Push
- Streaming Text Oriented Messaging Protocol (STOMP)
Favorite site
- Wikipedia (en) 웹소켓에 대한 설명
- [추천] Joinc - Websocket
- [추천] websocket.org Echo Test - Powered by Kaazing
- HTML5: WEB SOCKET (웹 소켓)
- WebSocket과 Socket.io
- webSocket 으로 개발하기 전에 알고 있어야 할 것들 1 (양방향 통신 방법들 ~ polling, long polling, etc ~ 에 대한 설명)
- See also: Pull technology
- WebSocket Protocol 스펙 메모
- MDN - 웹소켓 서버 작성하기
- HTML5 Developer :: Web Socket 서버 구현
- CodeProject - HTML5 Web Socket in Essence
- tutorialspoint - HTML5 - WebSockets
- javascript.info - websocket
- 자바스크립트는 어떻게 작동하는가: 웹소켓 및 HTTP/2 SSE
- 10.5 Node.js(Express)와 Socket.io - Socket.io를 사용한 실시간 채팅 애플리케이션
- (FastAPI/Python) 양방향 통신을 위한 웹소켓 in FastAPI — Dev In Seoul (FastAPI, WebSocket, broadcaster, redis)
References
-
Things_you_should_know_before_developing_with_webSocket.pdf ↩