Skip to content

hisune/websocket

master
Switch branches/tags

Name already in use

A tag already exists with the provided branch name. Many Git commands accept both tag and branch names, so creating this branch may cause unexpected behavior. Are you sure you want to create this branch?
Code

Latest commit

 

Git stats

Files

Permalink
Failed to load latest commit information.
Type
Name
Latest commit message
Commit time
 
 
lib
 
 
 
 
 
 
 
 
 
 

Nodejs Websocket server

What is this?

这是一个简单的websocket协议服务端实现。

This is a simple demo about websocket protocol.

数据分片、多数据包数据也已经实现!

Support data fragmentation.

测试兼容Chrome/Firefox/IE11。

Compatible Chrome / Firefox / IE10+.

How to use?

node app

A simple client?

Note: In Chrome, you must open it in nginx or apache service.

<input id="msg" />
<button onclick="send()">send</button>
<div id="content"></div>
if ("WebSocket" in window) {
	console.log("WebSocket is supported by your Browser!");
	var ws = new WebSocket("ws://127.0.0.1:4000/");
	ws.onopen = function () {
		console.log('open');
	};
	ws.onmessage = function (msg) {
		var receive = msg.data;
		console.log("Message is received...");
		display(receive, false);
	};
	ws.onclose = function () {
		console.log("Connection is closed...");
	};
} else {
	console.log("WebSocket NOT supported by your Browser!");
}

function send()
{
	console.log("Message is sent...");
	var str = document.getElementById('msg').value;
	console.log(str.length);
	ws.send(str);
	display(str, true);
	document.getElementById('msg').value = '';
}

function display(msg, send)
{
	var div = document.createElement('div');

	if(send)
		div.innerHTML = 'Me: ' + msg;
	else
		div.innerHTML = 'Somebody: ' + msg;

	document.getElementById('content').appendChild(div);
}

(http://hisune.com/view/36/websocket-protocol-nodejs-server-client)

Code by Hisune lyx