使用 weaving-socket教学-入门socket与websocket

首先把项目下载下来: 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请务必注意。

普通的socket

新版本更新后MyInterface 变更命名WeaveBase。TCPCommand变更命名,WeaveTCPCommand请务必注意。

一个通信项目需要服务端与客户端两部分,我们先开始写一个服务端。

服务端:

创建一个控制台程序,引用类库 MyInterface与TCPServer

然后编写代码

static void Main(string[] args)
        {
            p2psever server = new p2psever();//初始化类库
            server.receiveevent += Server_receiveevent;//注册接收事件
//当然还有很多其他的事件可以注册,比如新增连接事件,连接断开事件
            server.start(8989);//启动监听8989端口
             
           
            Console.WriteLine("8989listen:");
            Console.ReadKey();
        }

        private static void Server_receiveevent(byte command, string data, System.Net.Sockets.Socket soc)
        {
            Console.WriteLine(data);//输出客户端发来的信息
        }

客户端:

然后创建一个控制台程序,引用类库 MyInterfaceTCPclient

然后编写代码

   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服务器的8989端口。不需要服务器TOKEN
            System.Threading.Thread.Sleep(1000);
            Console.WriteLine("server link OK:");
            client.send(0x1, "test2017-5-5");//给服务器发送信息,参数1,0x01指令,指令可以设置0-254,其中0x9c与0xff,是保留指令不能使用。参数2:发送string类型的数据。
            Console.WriteLine("send:test2017-5-5");
            Console.ReadKey();
        }

        private static void Client_receiveServerEvent(byte command, string text)
        {
          //command是从服务器发来的指令
          //text是从服务器发来的数据
        }

        private static void Client_timeoutevent()
        {
         //连接超时或断线会启动此事件
            client。Restart(false);//重新连接
        }

最后:先运行服务器端,在运行客户端,就能在服务器端看到 test2017-5-5 的输出内容。

websocket

服务端:服务端内容与一般的socket,一模一样,只需要把p2psever server = new p2psever(); 这句话换成

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

客户端:

在项目中创建一个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;
        // 连接服务端
        function connect() {
             // ip: '127.0.0.1', port: 8989, 要链接的服务器的IP与端口
                    //conn: 连接成功事件
                    //, recData: 接收数据事件
                    //, close: 连接关闭事件
                     //, error: 连接错误事件
                    //  , jump: 服务器资源超过最大上限,推荐跳转其他服务器的事件

            socket = new UDCsocket({
                ip: '127.0.0.1', port: 8989, conn: onopen
                    , recData: onmessage
                    , close: function () { alert("连接关闭"); }
                     , error: function (msg) { alert("连接错误" + msg); }
                      , jump: function (ip) { alert("服务器超过最大连接,请连接其他服务器:" + ip); }
            });
        }
 function onopen(msg) {//连接后启动一次
            if (msg == 'token') {//连接后如果收到token会再次启动这个方法msg 带有token标识
               
               
            }

        }
        function onmessage(text) {
           //text 就是服务器发来的数据
            
        }

这样就可以,就是这么简单,你学会了吗?

猜你喜欢

转载自my.oschina.net/u/2476624/blog/893639