create room, get room information from server
This commit is contained in:
@@ -47,11 +47,15 @@ namespace Client
|
||||
return;
|
||||
|
||||
LoginResponsePacket packet = (LoginResponsePacket)sender;
|
||||
MessageBox.Show(packet.Code.ToString());
|
||||
if (packet.Code != StatusCode.Success)
|
||||
if (packet.Code == StatusCode.Success)
|
||||
{
|
||||
RoomListForm roomListForm = new RoomListForm();
|
||||
roomListForm.ShowDialog();
|
||||
IAsyncResult ar = null;
|
||||
ar = BeginInvoke(() =>
|
||||
{
|
||||
RoomListForm roomListForm = new RoomListForm();
|
||||
roomListForm.ShowDialog();
|
||||
EndInvoke(ar);
|
||||
});
|
||||
}
|
||||
else
|
||||
{
|
||||
|
20
Chat/Client/RoomListForm.Designer.cs
generated
20
Chat/Client/RoomListForm.Designer.cs
generated
@@ -34,20 +34,21 @@
|
||||
this.btnCreate = new System.Windows.Forms.Button();
|
||||
this.label1 = new System.Windows.Forms.Label();
|
||||
this.label2 = new System.Windows.Forms.Label();
|
||||
this.btnRefresh = new System.Windows.Forms.Button();
|
||||
this.SuspendLayout();
|
||||
//
|
||||
// lbxRoom
|
||||
//
|
||||
this.lbxRoom.FormattingEnabled = true;
|
||||
this.lbxRoom.ItemHeight = 15;
|
||||
this.lbxRoom.Location = new System.Drawing.Point(12, 27);
|
||||
this.lbxRoom.Location = new System.Drawing.Point(12, 31);
|
||||
this.lbxRoom.Name = "lbxRoom";
|
||||
this.lbxRoom.Size = new System.Drawing.Size(180, 214);
|
||||
this.lbxRoom.TabIndex = 0;
|
||||
//
|
||||
// btnEnter
|
||||
//
|
||||
this.btnEnter.Location = new System.Drawing.Point(12, 247);
|
||||
this.btnEnter.Location = new System.Drawing.Point(12, 251);
|
||||
this.btnEnter.Name = "btnEnter";
|
||||
this.btnEnter.Size = new System.Drawing.Size(180, 23);
|
||||
this.btnEnter.TabIndex = 1;
|
||||
@@ -75,7 +76,7 @@
|
||||
// label1
|
||||
//
|
||||
this.label1.AutoSize = true;
|
||||
this.label1.Location = new System.Drawing.Point(12, 9);
|
||||
this.label1.Location = new System.Drawing.Point(12, 13);
|
||||
this.label1.Name = "label1";
|
||||
this.label1.Size = new System.Drawing.Size(59, 15);
|
||||
this.label1.TabIndex = 4;
|
||||
@@ -90,6 +91,16 @@
|
||||
this.label2.TabIndex = 5;
|
||||
this.label2.Text = "Creation";
|
||||
//
|
||||
// btnRefresh
|
||||
//
|
||||
this.btnRefresh.Location = new System.Drawing.Point(134, 5);
|
||||
this.btnRefresh.Name = "btnRefresh";
|
||||
this.btnRefresh.Size = new System.Drawing.Size(58, 23);
|
||||
this.btnRefresh.TabIndex = 1;
|
||||
this.btnRefresh.Text = "Refresh";
|
||||
this.btnRefresh.UseVisualStyleBackColor = true;
|
||||
this.btnRefresh.Click += new System.EventHandler(this.btnRefresh_Click);
|
||||
//
|
||||
// RoomListForm
|
||||
//
|
||||
this.AutoScaleDimensions = new System.Drawing.SizeF(7F, 15F);
|
||||
@@ -99,6 +110,7 @@
|
||||
this.Controls.Add(this.label1);
|
||||
this.Controls.Add(this.btnCreate);
|
||||
this.Controls.Add(this.tbxRoomName);
|
||||
this.Controls.Add(this.btnRefresh);
|
||||
this.Controls.Add(this.btnEnter);
|
||||
this.Controls.Add(this.lbxRoom);
|
||||
this.FormBorderStyle = System.Windows.Forms.FormBorderStyle.FixedSingle;
|
||||
@@ -108,6 +120,7 @@
|
||||
this.StartPosition = System.Windows.Forms.FormStartPosition.CenterParent;
|
||||
this.Text = "Room list";
|
||||
this.Activated += new System.EventHandler(this.RoomListForm_Activated);
|
||||
this.FormClosing += new System.Windows.Forms.FormClosingEventHandler(this.RoomListForm_FormClosing);
|
||||
this.ResumeLayout(false);
|
||||
this.PerformLayout();
|
||||
|
||||
@@ -121,5 +134,6 @@
|
||||
private Button btnCreate;
|
||||
private Label label1;
|
||||
private Label label2;
|
||||
private Button btnRefresh;
|
||||
}
|
||||
}
|
@@ -1,9 +1,11 @@
|
||||
using System;
|
||||
using Core;
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.ComponentModel;
|
||||
using System.Data;
|
||||
using System.Drawing;
|
||||
using System.Linq;
|
||||
using System.Net.Sockets;
|
||||
using System.Text;
|
||||
using System.Threading.Tasks;
|
||||
using System.Windows.Forms;
|
||||
@@ -12,9 +14,18 @@ namespace Client
|
||||
{
|
||||
public partial class RoomListForm : Form
|
||||
{
|
||||
private delegate void InvokedMethod();
|
||||
|
||||
public RoomListForm()
|
||||
{
|
||||
InitializeComponent();
|
||||
InitInstance();
|
||||
}
|
||||
|
||||
private void InitInstance()
|
||||
{
|
||||
Singleton.Instance.CreateRoomResponsed += CreateRoomResponsed;
|
||||
Singleton.Instance.RoomListResponsed += Instance_RoomListResponsed;
|
||||
}
|
||||
|
||||
private void btnEnter_Click(object sender, EventArgs e)
|
||||
@@ -25,12 +36,17 @@ namespace Client
|
||||
return;
|
||||
}
|
||||
|
||||
ChatRoomForm chatRoomForm = new ChatRoomForm();
|
||||
chatRoomForm.Text = lbxRoom.SelectedItem.ToString();
|
||||
chatRoomForm.ShowDialog();
|
||||
IAsyncResult ar = null;
|
||||
ar = BeginInvoke(() =>
|
||||
{
|
||||
ChatRoomForm chatRoomForm = new ChatRoomForm();
|
||||
chatRoomForm.Text = lbxRoom.SelectedItem.ToString();
|
||||
chatRoomForm.ShowDialog();
|
||||
EndInvoke(ar);
|
||||
});
|
||||
}
|
||||
|
||||
private void btnCreate_Click(object sender, EventArgs e)
|
||||
private async void btnCreate_Click(object sender, EventArgs e)
|
||||
{
|
||||
string roomName = tbxRoomName.Text;
|
||||
if (string.IsNullOrEmpty(roomName))
|
||||
@@ -39,14 +55,83 @@ namespace Client
|
||||
return;
|
||||
}
|
||||
|
||||
ChatRoomForm chatRoomForm = new ChatRoomForm();
|
||||
chatRoomForm.Text = roomName;
|
||||
chatRoomForm.ShowDialog();
|
||||
CreateRoomRequestPacket packet = new CreateRoomRequestPacket(roomName);
|
||||
await Singleton.Instance.SendAsync(packet.Serialize(), SocketFlags.None);
|
||||
}
|
||||
|
||||
private void RoomListForm_Activated(object sender, EventArgs e)
|
||||
{
|
||||
btnRefresh_Click(sender, e);
|
||||
}
|
||||
|
||||
private void InvokeMethod(InvokedMethod method)
|
||||
{
|
||||
if (InvokeRequired)
|
||||
{
|
||||
lbxRoom.Invoke(new MethodInvoker(() =>
|
||||
{
|
||||
method();
|
||||
}));
|
||||
}
|
||||
else
|
||||
{
|
||||
method();
|
||||
}
|
||||
}
|
||||
|
||||
private void CreateRoomResponsed(object? sender, EventArgs e)
|
||||
{
|
||||
if (sender == null)
|
||||
return;
|
||||
|
||||
CreateRoomResponsePacket packet = (CreateRoomResponsePacket)sender;
|
||||
if (packet.Code == StatusCode.Success)
|
||||
{
|
||||
string roomName = tbxRoomName.Text;
|
||||
InvokeMethod(() =>
|
||||
{
|
||||
lbxRoom.Items.Add(roomName);
|
||||
tbxRoomName.Text = "";
|
||||
});
|
||||
|
||||
IAsyncResult ar = null;
|
||||
ar = BeginInvoke(() =>
|
||||
{
|
||||
ChatRoomForm chatRoomForm = new ChatRoomForm();
|
||||
chatRoomForm.Text = roomName;
|
||||
chatRoomForm.ShowDialog();
|
||||
EndInvoke(ar);
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
private void Instance_RoomListResponsed(object? sender, EventArgs e)
|
||||
{
|
||||
if (sender == null)
|
||||
return;
|
||||
|
||||
RoomListResponsePacket packet = (RoomListResponsePacket)sender;
|
||||
InvokeMethod(() =>
|
||||
{
|
||||
foreach (string item in packet.RoomNames)
|
||||
{
|
||||
lbxRoom.Items.Add(item);
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
private async void btnRefresh_Click(object sender, EventArgs e)
|
||||
{
|
||||
lbxRoom.Items.Clear();
|
||||
RoomListRequestPacket packet = new RoomListRequestPacket();
|
||||
await Singleton.Instance.SendAsync(packet.Serialize(), SocketFlags.None);
|
||||
}
|
||||
|
||||
private void RoomListForm_FormClosing(object sender, FormClosingEventArgs e)
|
||||
{
|
||||
Singleton.Instance.CreateRoomResponsed -= CreateRoomResponsed;
|
||||
Singleton.Instance.RoomListResponsed -= Instance_RoomListResponsed;
|
||||
Singleton.Instance.Socket.Shutdown(SocketShutdown.Send);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@@ -18,6 +18,8 @@ namespace Client
|
||||
public Socket Socket { get; } = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
|
||||
|
||||
public event EventHandler? LoginResponsed;
|
||||
public event EventHandler? CreateRoomResponsed;
|
||||
public event EventHandler? RoomListResponsed;
|
||||
|
||||
public async Task ConnectAsync(string ip, int port)
|
||||
{
|
||||
@@ -70,6 +72,16 @@ namespace Client
|
||||
LoginResponsePacket packet = new LoginResponsePacket(dataBuffer);
|
||||
LoginResponsed?.Invoke(packet, EventArgs.Empty);
|
||||
}
|
||||
else if (packetType == PacketType.CreateRoomResponse)
|
||||
{
|
||||
CreateRoomResponsePacket packet = new CreateRoomResponsePacket(dataBuffer);
|
||||
CreateRoomResponsed?.Invoke(packet, EventArgs.Empty);
|
||||
}
|
||||
else if (packetType == PacketType.RoomListResponse)
|
||||
{
|
||||
RoomListResponsePacket packet = new RoomListResponsePacket(dataBuffer);
|
||||
RoomListResponsed?.Invoke(packet, EventArgs.Empty);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
Reference in New Issue
Block a user