Created
August 19, 2013 16:56
-
-
Save castaneai/6271377 to your computer and use it in GitHub Desktop.
ChatApp.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using System; | |
using System.Threading.Tasks; | |
using System.Windows.Forms; | |
using System.Net; | |
namespace WindowsFormsApplication1 | |
{ | |
public partial class Form1 : Form | |
{ | |
private Client client; | |
public Form1() | |
{ | |
InitializeComponent(); | |
} | |
/// <summary> | |
/// 127.0.0.1:10000 のような文字列をIPEndPointに変換する | |
/// </summary> | |
/// <param name="value"></param> | |
/// <returns></returns> | |
private IPEndPoint parseEP(string value) | |
{ | |
var hostname = value.Split(':')[0]; | |
var port = ushort.Parse(value.Split(':')[1]); | |
return new IPEndPoint(IPAddress.Parse(hostname), port); | |
} | |
/// <summary> | |
/// 主催者として接続するボタンを押したとき | |
/// </summary> | |
/// <param name="sender"></param> | |
/// <param name="e"></param> | |
private void connectAsHostButton_Click(object sender, EventArgs e) | |
{ | |
var bindEP = parseEP(this.endPointTextBox.Text); | |
this.client = Client.ConnectAsHost(bindEP); | |
serveForever(); | |
this.appendToLog("主催者として接続完了"); | |
} | |
/// <summary> | |
/// 客として接続するボタンを押したとき | |
/// </summary> | |
/// <param name="sender"></param> | |
/// <param name="e"></param> | |
private void connectAsGuestButton_Click(object sender, EventArgs e) | |
{ | |
var hostEP = parseEP(this.endPointTextBox.Text); | |
this.client = Client.ConnectAsGuest(hostEP); | |
serveForever(); | |
this.appendToLog("客として接続完了"); | |
} | |
/// <summary> | |
/// 相手からのメッセージをずっと受信し続ける(別スレッドで) | |
/// </summary> | |
private void serveForever() | |
{ | |
Task.Run(() => { | |
while (true) { | |
var receivedMessage = this.client.ReceiveMessage(); | |
this.appendToLog("相手:" + receivedMessage); | |
} | |
}); | |
} | |
/// <summary> | |
/// チャットログに一行追加する | |
/// </summary> | |
/// <param name="message"></param> | |
private void appendToLog(string message) | |
{ | |
this.Invoke((MethodInvoker)delegate() { | |
this.chatLog.AppendText(message + "\r\n"); | |
}); | |
} | |
/// <summary> | |
/// 入力欄にあるメッセージを相手に送信する | |
/// </summary> | |
private void sendMessage() | |
{ | |
if (this.client == null) { | |
MessageBox.Show("先に接続してください"); | |
return; | |
} | |
var message = this.messageInputTextBox.Text; | |
this.client.SendMessage(message); | |
this.appendToLog("あなた:" + message); | |
this.messageInputTextBox.Text = string.Empty; | |
} | |
/// <summary> | |
/// 送信ボタンを押したとき | |
/// </summary> | |
/// <param name="sender"></param> | |
/// <param name="e"></param> | |
private void sendButton_Click(object sender, EventArgs e) | |
{ | |
this.sendMessage(); | |
} | |
/// <summary> | |
/// 入力欄でEnterキーを押したとき | |
/// </summary> | |
/// <param name="sender"></param> | |
/// <param name="e"></param> | |
private void messageInputTextBox_KeyPress(object sender, KeyPressEventArgs e) | |
{ | |
if (e.KeyChar == (char)Keys.Enter) { | |
this.sendMessage(); | |
} | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment