WebSocket
lastmod 2025-04-14created 2015-11-1021 edits
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 (used by 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) explanation of WebSocket
- [Recommended] Joinc - Websocket
- [Recommended] websocket.org Echo Test - Powered by Kaazing
- HTML5: WEB SOCKET
- WebSocket and Socket.io
- Things you should know before developing with webSocket 1 (explanation of bidirectional communication methods ~ polling, long polling, etc ~)
- See also: Pull technology
- [Original] You might not need Websockets
References
-
Things_you_should_know_before_developing_with_webSocket.pdf ↩
Pages that link here
- Broadcaster
- Communication protocol
- ContinuousIntegration:ImplementationGuide
- Datahike
- DriftDB
- Eventkit
- FastAPI
- HyperText Markup Language
- Nginx:ReverseProxy
- Programming
- Protocol
- Push technology
- Python:Libraries
- Redis
- Streamedian
- Streaming Text Oriented Messaging Protocol
- Virtual Private Network
- Web API