基本信息
源码名称:SuperWebSocket与WebSocket4Net通信示例源码
源码大小:1.39M
文件格式:.rar
开发语言:C#
更新时间:2020-07-15
友情提示:(无需注册或充值,赞助后即可获取资源下载链接)
嘿,亲!知识可是无价之宝呢,但咱这精心整理的资料也耗费了不少心血呀。小小地破费一下,绝对物超所值哦!如有下载和支付问题,请联系我们QQ(微信同号):78630559
本次赞助数额为: 10 元×
微信扫码支付:10 元
×
请留下您的邮箱,我们将在2小时内将文件发到您的邮箱
源码介绍
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Configuration;
using SuperSocket.Common;
using SuperSocket.SocketBase;
using SuperSocket.SocketBase.Config;
using SuperSocket.SocketEngine;
using SuperSocket.SocketBase.Logging;
using SuperWebSocket;
using log4net;
namespace Server
{
public partial class FrmMain : Form
{
WebSocketServer appServer = new WebSocketServer();
ServerConfig serverConfig = new ServerConfig
{
Port = 2015,//set the listening port
MaxConnectionNumber = 10000
};
public FrmMain()
{
InitializeComponent();
}
private void FrmMain_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("连接成功");
listBox1.Items.Add("Host:" session.Host);//服务器的ip
listBox1.Items.Add("Uri:" session.UriScheme);
listBox1.Items.Add("Path:" session.Path);
listBox1.Items.Add("CurrentToken:" session.CurrentToken);
listBox1.Items.Add("SessionID:" session.SessionID);
listBox1.Items.Add("Connection" session.Connection);
listBox1.Items.Add("Origin" session.Origin);
listBox1.Items.Add("LocalEndPoint" session.LocalEndPoint);
listBox1.Items.Add("RemoteEndPoint" session.RemoteEndPoint);
}
void appServer_NewMessageReceived(WebSocketSession session, string value)
{
session.Send("服务端收到了客户端发来的消息");
this.listBox1.Items.Add(value);
}
void appServer_SessionClosed(WebSocketSession session, SuperSocket.SocketBase.CloseReason value)
{
this.listBox1.Items.Add(value.ToString());
session.Close();
}
}
}