教學(xué)-入門socket與websocket

2018-11-27 14:05 更新

首先把項目下載下來: http://git.oschina.net/dreamsfly900/universal-Data-Communication-System-for-windows

還有一個同胞項目

.NET Core的weaving-socket項目

http://git.oschina.net/dreamsfly900/weaving-socket-core

新版本更新后MyInterface 變更命名WeaveBase。TCPCommand變更命名,WeaveTCPCommand請務(wù)必注意。

普通的socket

新版本更新后MyInterface 變更命名WeaveBase。TCPCommand變更命名,WeaveTCPCommand請務(wù)必注意。

一個通信項目需要服務(wù)端與客戶端兩部分,我們先開始寫一個服務(wù)端。

服務(wù)端:

創(chuàng)建一個控制臺程序,引用類庫 MyInterface與TCPServer

然后編寫代碼

static void Main(string[] args) { p2psever server = new p2psever();//初始化類庫 server.receiveevent += Server_receiveevent;//注冊接收事件 //當然還有很多其他的事件可以注冊,比如新增連接事件,連接斷開事件 server.start(8989);//啟動監(jiān)聽8989端口

         

       
        Console.WriteLine("8989listen:");
        Console.ReadKey();
    }

private static void Server_receiveevent(byte command, string data, System.Net.Sockets.Socket soc) { Console.WriteLine(data);//輸出客戶端發(fā)來的信息 } 客戶端:

然后創(chuàng)建一個控制臺程序,引用類庫 MyInterface與TCPclient

然后編寫代碼

P2Pclient client = new P2Pclient(false);//初始化類庫 static void Main(string[] args) {

       
        client.timeoutevent += Client_timeoutevent;//注冊連接超時事件
        client.receiveServerEvent += Client_receiveServerEvent;//注冊接收事件
          client.start("127.0.0.1", 8989, false);//啟動連接127.0.0.1服務(wù)器的8989端口。不需要服務(wù)器TOKEN
        System.Threading.Thread.Sleep(1000);
        Console.WriteLine("server link OK:");
        client.send(0x1, "test2017-5-5");//給服務(wù)器發(fā)送信息,參數(shù)1,0x01指令,指令可以設(shè)置0-254,其中0x9c與0xff,是保留指令不能使用。參數(shù)2:發(fā)送string類型的數(shù)據(jù)。
        Console.WriteLine("send:test2017-5-5");
        Console.ReadKey();
    }

private static void Client_receiveServerEvent(byte command, string text) { //command是從服務(wù)器發(fā)來的指令 //text是從服務(wù)器發(fā)來的數(shù)據(jù) }

private static void Client_timeoutevent() { //連接超時或斷線會啟動此事件 client。Restart(false);//重新連接 }

最后:先運行服務(wù)器端,在運行客戶端,就能在服務(wù)器端看到 test2017-5-5 的輸出內(nèi)容。

websocket

服務(wù)端:服務(wù)端內(nèi)容與一般的socket,一模一樣,只需要把p2psever server = new p2psever(); 這句話換成

Webp2psever server = new Webp2psever(); 就可以了,其他的用法與方法與p2psever 完全一致,都是繼承與ITcpBasehelper接口

客戶端:

在項目中創(chuàng)建一個web項目,新建一個html文件

在HTML文件中引用

<script src="websocket.js"></script> <script src="scripts/jquery-1.4.1.min.js"></script> 這兩個文件,websocket.js在項目前端WEB示例中可以找到,或者文件夾WebApplication1 下面也可以找到。

編寫JS代碼

var socket; // 連接服務(wù)端 function connect() { // ip: '127.0.0.1', port: 8989, 要鏈接的服務(wù)器的IP與端口 //conn: 連接成功事件 //, recData: 接收數(shù)據(jù)事件 //, close: 連接關(guān)閉事件 //, error: 連接錯誤事件 // , jump: 服務(wù)器資源超過最大上限,推薦跳轉(zhuǎn)其他服務(wù)器的事件

socket = new UDCsocket({ ip: '127.0.0.1', port: 8989, conn: onopen , recData: onmessage , close: function () { alert("連接關(guān)閉"); } , error: function (msg) { alert("連接錯誤" + msg); } , jump: function (ip) { alert("服務(wù)器超過最大連接,請連接其他服務(wù)器:" + ip); } }); } function onopen(msg) {//連接后啟動一次 if (msg == 'token') {//連接后如果收到token會再次啟動這個方法msg 帶有token標識

           

           
        }

} function onmessage(text) { //text 就是服務(wù)器發(fā)來的數(shù)據(jù)

        
    }

這樣就可以,就是這么簡單,你學(xué)會了嗎?

以上內(nèi)容是否對您有幫助:
在線筆記
App下載
App下載

掃描二維碼

下載編程獅App

公眾號
微信公眾號

編程獅公眾號