简体   繁体   English

Windows Phone 8.1 IRC

[英]Windows Phone 8.1 IRC

I've been trying to make an IRC client for my Windows Phone 8.1 app, and I was lucky enough to find a really good tutorial. 我一直在尝试为Windows Phone 8.1应用程序制作IRC客户端,我很幸运地找到了一个非常好的教程。 Unfortunately the tutorial was for WP 7 and as of WP 8.1 MS changed it to runtime apps, meaning SocketAsyncEvents is unavailable to me (even though MSDN says it supports Windows Phone 8.1). 不幸的是,该教程适用于WP 7,自WP 8.1起,MS将其更改为运行时应用程序,这意味着SocketAsyncEvents对我不可用(即使MSDN说它支持Windows Phone 8.1)。

public void SendToServer(string message)
{
    var asyncEvent = new SocketAsyncEventArgs { RemoteEndPoint = new DnsEndPoint(server, serverPort) };

    var buffer = Encoding.UTF8.GetBytes(message + Environment.NewLine);
    asyncEvent.SetBuffer(buffer, 0, buffer.Length);

    connection.SendAsync(asyncEvent);
}

Digging around I found that the sockets were moved to Windows.Networking.Sockets, but yet none of them contained SocketAsyncEvents . 到处逛逛,我发现套接字已移至Windows.Networking.Sockets,但它们都不包含SocketAsyncEvents

I'm pretty much unable to go any further from here, does anybody have an idea on how to convert said function to something that would work with WP 8.1? 我几乎无法从这里进行进一步的介绍,有人对如何将所述函数转换为可与WP 8.1兼容的方法有任何想法吗?

Here goes! 开始!

After a lot of research this is what I found: 经过大量研究,这是我发现的:

First of all we got a connect method. 首先,我们有一个connect方法。

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;

}

To read the data we receive through the StreamSocket, we create a ReadData() method and make it recursive so we will continue to get the data: 要读取通过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();
}

Now we have two new methods, SendIdentity(); 现在我们有了两个新方法, SendIdentity(); and ReplyPong(string message); ReplyPong(string message); Usually the IRC server will ping you, here you have to reply with a pong, like so: 通常IRC服务器会ping您,在这里您必须用pong进行回复,如下所示:

private void ReplyPong(string message) {
    var pingCode = Regex.Match(message, "[0-9]+");
    SendRawMessage("PONG :" + pingCode);
}

And we also have to send our identity when the server is ready for it, like so: 当服务器准备就绪时,我们还必须发送我们的身份,例如:

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;
    }
}

At last we have our SendRawMessage(); 最后,我们有了SendRawMessage(); method, that sends data to the server. 方法,将数据发送到服务器。

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;
}

Almost forgot out dispose function, which you can call when you want to close the stream :) 几乎忘记了dispose函数,当您要关闭流时可以调用它:)

public void Dispose()
{
     SendRawMessage("QUIT :");
     _closing = true;
}

This will send a last message indicating that we're leaving, and since _closing now is true, the stream will be disposed afterwards. 这将发送一条最后一条消息,指示我们要离开,并且由于_closing现在为true,因此此流将在之后被释放。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM