C# SuperWebSocket服务端、客户端学习(三)

1、打开VS2012,新建一个windows窗体程序,选择.NET4.0版本

2、添加引用    

SuperSocket的dll文件(

SuperSocket.Common.dll,

SuperSocket.SocketBase.dll,

SuperSocket.SocketEngine.dll)到此项目的引用 (版本选4.0)

SuperWebSocket.dll   到此项目的引用

添加 系统:
System.Configuration;

System.Configuration.Install;  到此项目的引用

3、具体代码实现如下所示(由于项目的特殊性,不能完全展示出来,请见谅):

(1)服务器端:

using SuperSocket.SocketBase.Config;
using SuperWebSocket;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace ServerSuperWebSocket
{
    public partial class Form1 : Form
    {
        ///
        ///2016-8-2  服务器打开及接受消息
        ///

        ///

        private WebSocketServer appServer = new WebSocketServer();
      
        ServerConfig serverConfig = new ServerConfig
        {
            Port = 2015,//set the listening port
            MaxConnectionNumber = 10000
        };


        public Form1()
        {
            InitializeComponent();
        }

       // private WebSocketServer appServer = WebSocketHelper.get().getAppServer(); //封装之后添加的
        private void Form1_Load(object sender, EventArgs e)
        {
            if (!appServer.Setup(serverConfig)) //Setup the appServer
            {
                System.Windows.Forms.MessageBox.Show("开启服务器失败");
                return;
            }

            if (!appServer.Start())//Try to start the appServer
            {
                System.Windows.Forms.MessageBox.Show("开启服务器失败");
                return;
            }
            //注册事件
            appServer.NewSessionConnected += appServer_NewSessionConnected;//客户端连接
            appServer.NewMessageReceived += appServer_NewMessageReceived;//客户端接收消息
            appServer.SessionClosed += appServer_SessionClosed;//客户端关闭
        }

        void appServer_NewSessionConnected(WebSocketSession session)
        {
            //session.Send("连接成功");
            StringBuilder strBuilder = new StringBuilder();
            strBuilder.Append("连接成功:" + "\n");
            strBuilder.Append("Host:" + session.Host + ";");//服务器的ip
            strBuilder.Append("Uri:" + session.UriScheme + ";");
            strBuilder.Append("Path:" + session.Path + ";");
            strBuilder.Append("CurrentToken:" + session.CurrentToken + ";");
            strBuilder.Append("SessionID:" + session.SessionID + ";");
            strBuilder.Append("Connection" + session.Connection + ";");
            strBuilder.Append("Origin" + session.Origin + ";");
            strBuilder.Append("LocalEndPoint" + session.LocalEndPoint + ";");
            strBuilder.Append("RemoteEndPoint" + session.RemoteEndPoint);
      

        }
        void appServer_NewMessageReceived(WebSocketSession session, string message)
        {
            session.Send("服务端收到了客户端发来的消息");
            //这里判断接收消息
            //session.Send(message);//将消息发送到客户端
           // SessionResponse.get().InitResponseJsonData(message);  //解析数据(将数据保存到数据库中)
            //WebSocketHelper.get().setWebSocketSession(session); //将这个session传给session
            //调用发送消息的类
           // SessionRequest.get().sendUserInfo();

        }
        void appServer_SessionClosed(WebSocketSession session, SuperSocket.SocketBase.CloseReason value)
        {
            sessionManager.Remove(session.SessionID.ToString());
            session.Close();
        }
    }
}

(2)客户端(同样需要建立windows程序)

C# SuperWebSocket服务端、客户端学习(三)_第1张图片

程序如下:

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using WebSocket4Net;

namespace ClientSuperWebSocket
{
    public partial class Form1 : Form
    {
        private WebSocket websocket = null;
        private string strSendText = string.Empty;
        public Form1()
        {
            InitializeComponent();
        }

        private void Form1_Load(object sender, EventArgs e)
        {
            websocket = new WebSocket("ws://127.0.0.1:2015");
            WebSocketHelper.get().setWebSocket(websocket);

            websocket.Opened += websocket_Opened;
            websocket.Closed += websocket_Closed;
            websocket.MessageReceived += websocket_MessageReceived;
            websocket.Open();
        }

        private void websocket_MessageReceived(object sender, MessageReceivedEventArgs e)
        {
            //this.listBox1.Invoke(new EventHandler(ShowMessage), e.Message);
            MessageReceivedEventArgs responseMsg = (MessageReceivedEventArgs)e; //接收服务端发来的消息
            string strMsg = responseMsg.Message;
        }

        private void ShowMessage(object sender, EventArgs e)
        {
            this.listBox1.Items.Add(sender.ToString());
        }

        private void websocket_Closed(object sender, EventArgs e)
        {
            //websocket.Send("一个客户端 下线");
        }

        void websocket_Opened(object sender, EventArgs e)
        {
            //websocket.Send("一个客户端 上线");
        }

        private void button1_Click(object sender, EventArgs e)
        {
            //strSendText = textBox1.Text.ToString();
            //websocket.Send(strSendText);
            WebSocketRequestHelper.get().Register("18630635489", "123456");
        }

    }
}





你可能感兴趣的:(C# SuperWebSocket服务端、客户端学习(三))