node.js中ws模塊創(chuàng)建服務(wù)端和客戶端,網(wǎng)頁(yè)WebSocket客戶端
首先下載websocket模塊,命令行輸入
npm install ws
1.node.js中ws模塊創(chuàng)建服務(wù)端
// 加載node上websocket模塊 ws;
var ws = require("ws");
// 啟動(dòng)基于websocket的服務(wù)器,監(jiān)聽(tīng)我們的客戶端接入進(jìn)來(lái)。
var server = new ws.Server({
host: "127.0.0.1",
port: 6080,
});
// 監(jiān)聽(tīng)接入進(jìn)來(lái)的客戶端事件
function websocket_add_listener(client_sock) {
// close事件
client_sock.on("close", function() {
console.log("client close");
});
// error事件
client_sock.on("error", function(err) {
console.log("client error", err);
});
// end
// message 事件, data已經(jīng)是根據(jù)websocket協(xié)議解碼開(kāi)來(lái)的原始數(shù)據(jù);
// websocket底層有數(shù)據(jù)包的封包協(xié)議,所以,絕對(duì)不會(huì)出現(xiàn)粘包的情況。
// 每解一個(gè)數(shù)據(jù)包,就會(huì)觸發(fā)一個(gè)message事件;
// 不會(huì)出現(xiàn)粘包的情況,send一次,就會(huì)把send的數(shù)據(jù)獨(dú)立封包。
// 如果我們是直接基于TCP,我們要自己實(shí)現(xiàn)類似于websocket封包協(xié)議就可以完全達(dá)到一樣的效果;
client_sock.on("message", function(data) {
console.log(data);
client_sock.send("Thank you!");
});
// end
}
// connection 事件, 有客戶端接入進(jìn)來(lái);
function on_server_client_comming (client_sock) {
console.log("client comming");
websocket_add_listener(client_sock);
}
server.on("connection", on_server_client_comming);
// error事件,表示的我們監(jiān)聽(tīng)錯(cuò)誤;
function on_server_listen_error(err) {
}
server.on("error", on_server_listen_error);
// headers事件, 回給客戶端的字符。
function on_server_headers(data) {
// console.log(data);
}
server.on("headers", on_server_headers);
2.node.js中ws模塊創(chuàng)建客戶端
var ws = require("ws");
// url ws://127.0.0.1:6080
// 創(chuàng)建了一個(gè)客戶端的socket,然后讓這個(gè)客戶端去連接服務(wù)器的socket
var sock = new ws("ws://127.0.0.1:6080");
sock.on("open", function () {
console.log("connect success !!!!");
sock.send("HelloWorld1");
sock.send("HelloWorld2");
sock.send("HelloWorld3");
sock.send("HelloWorld4");
sock.send(Buffer.alloc(10));
});
sock.on("error", function(err) {
console.log("error: ", err);
});
sock.on("close", function() {
console.log("close");
});
sock.on("message", function(data) {
console.log(data);
});
3.網(wǎng)頁(yè)客戶端創(chuàng)建(使用WebApi --->WebSocket)
<!DOCTYPE html>
<html>
<head>
<title>skynet websocket example</title>
</head>
<body>
<script>
var ws = new WebSocket("ws://127.0.0.1:6080/index.html");
ws.onopen = function(){
alert("open");
ws.send("WebSocket hellowrold!!");
};
ws.onmessage = function(ev){
alert(ev.data);
};
ws.onclose = function(ev){
alert("close");
};
ws.onerror = function(ev){
console.log(ev);
alert("error");
};
</script>
</body>
</html>
總結(jié)
以上就是這篇文章的全部?jī)?nèi)容了,希望本文的內(nèi)容對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,謝謝大家對(duì)腳本之家的支持。如果你想了解更多相關(guān)內(nèi)容請(qǐng)查看下面相關(guān)鏈接
相關(guān)文章
nodejs中使用worker_threads來(lái)創(chuàng)建新的線程的方法
這篇文章主要介紹了nodejs中使用worker_threads來(lái)創(chuàng)建新的線程的方法,本文給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2021-01-01
node.js中的path.join方法使用說(shuō)明
這篇文章主要介紹了node.js中的path.join方法使用說(shuō)明,本文介紹了path.join的方法說(shuō)明、語(yǔ)法、使用實(shí)例和實(shí)現(xiàn)源碼,需要的朋友可以參考下2014-12-12
解決node終端下運(yùn)行js文件不支持ES6語(yǔ)法
這篇文章主要介紹了解決node終端下運(yùn)行js文件不支持ES6語(yǔ)法,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2020-04-04
圖解NodeJS實(shí)現(xiàn)登錄注冊(cè)功能
這篇文章主要介紹了NodeJS實(shí)現(xiàn)登錄注冊(cè)功能,本文圖文并茂給大家介紹的非常詳細(xì),具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2019-09-09
Node.js dgram模塊實(shí)現(xiàn)UDP通信示例代碼
這篇文章主要介紹了Node.js dgram模塊實(shí)現(xiàn)UDP通信示例代碼,小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過(guò)來(lái)看看吧2017-09-09
node.js通過(guò)Sequelize 連接MySQL的方法
這篇文章主要介紹了node.js通過(guò)Sequelize 連接MySQL的方法,本文給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作,具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2020-12-12
關(guān)于Node.js的events.EventEmitter用法介紹
本篇文章主要介紹了關(guān)于Node.js的events.EventEmitter用法,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下。2017-04-04

