這裏去!
大量的研究後,這是我發現:
首先我們得到了一個連接方法。
private readonly StreamSocket _clientSocket;
private bool _connected;
private DataReader _dataReader;
public string Hostname {
get;
set;
}
public int Port {
get;
set;
}
public Credentials Credentials;
public readonly string Channel;
public async Task <bool> Connect() {
if (_connected) return false;
var hostname = new HostName(Hostname);
await _clientSocket.ConnectAsync(hostname, Port.ToString());
_connected = true;
_dataReader = new DataReader(_clientSocket.InputStream) {
InputStreamOptions = InputStreamOptions.Partial
};
ReadData();
return true;
}
要閱讀我們通過StreamSocket接收到的數據,我們創建了一個READDATA()方法,使之遞歸因此我們將繼續得到數據:
async private void ReadData() {
if (!_connected || _clientSocket == null) return;
uint s = await _dataReader.LoadAsync(2048);
string data = _dataReader.ReadString(s);
if (data.Contains("No ident response")) SendIdentity();
if (Regex.IsMatch(data, "PING :[0-9]+\\r\\n")) ReplyPong(data);
ReadData();
}
現在我們有兩個新方法,SendIdentity();
和ReplyPong(string message);
通常IRC服務器將平你,這裏你有一個乒乓球回覆,就像這樣:
private void ReplyPong(string message) {
var pingCode = Regex.Match(message, "[0-9]+");
SendRawMessage("PONG :" + pingCode);
}
而且我們還要送我們的身份時,該服務器已經準備好了,就像這樣:
private void SendIdentity() {
if (Credentials.Nickname == string.Empty) Credentials.Nickname = Credentials.Username;
SendRawMessage("NICK " + Credentials.Nickname);
SendRawMessage("USER " + Credentials.Username + " " + Credentials.Username + " " + Credentials.Username + " :" + Credentials.Username);
if (Credentials.Password != String.Empty) SendRawMessage("PASS " + Credentials.Password);
}
public class Credentials {
public string Nickname {
get;
set;
}
public string Username {
get;
set;
}
public string Password {
get;
set;
}
public Credentials(string username, string password = "", string nickname = "") {
Username = username;
Password = password;
Nickname = nickname;
}
}
最後我們有SendRawMessage();
方法,將數據發送到服務器。
async private void SendRawMessage(string message) {
var writer = new DataWriter(_clientSocket.OutputStream);
writer.WriteString(message + "\r\n");
await writer.StoreAsync();
await writer.FlushAsync();
writer.DetachStream();
if (!_closing) return;
_clientSocket.DisposeSafe();
_connected = false;
}
差點忘了處置功能,當你想關閉該流:)
public void Dispose()
{
SendRawMessage("QUIT :");
_closing = true;
}
這將發送指示最後的消息,我們要走了,既然現在_closing你可以調用這是真的,這條河將在事後處理。
您的項目是Windows Phone 8.1 Runtime(通用應用程序)或Windows 8.1 silverlight? – crea7or 2014-11-22 23:29:07
我找到了一個解決方案,只要我爲它寫了一個類,我就會更新這篇文章:)感謝您對此感興趣。是的,它是運行時間:) – Jazerix 2014-11-23 01:22:24