Websockets消息未被发送

时间:2014-02-26 13:55:44

标签: c# .net websocket windows-media-player

这是我正在处理的Windows Media Player C#代码,它将TCP WebSocket消息发送回客户端,说明mp3音频仍在播放。

我正在使用axWindowsMediaPlayer1.StatusChangeStatusChange会相应地触发消息,但即使我可以在我的WMP C#应用程序中接收来自客户端的消息。

我无法弄清楚为什么它不会将var message_back = "Audio still Playing...";发送回客户端。

private void Form1_Load(object sender, EventArgs e)
{
    lblMessage.Text = "";
    axWindowsMediaPlayer1.URL = @"C:\Audio\song1.MP3";
    axWindowsMediaPlayer1.Ctlcontrols.stop();

    this.axWindowsMediaPlayer1.StatusChange += new EventHandler(axWindowsMediaPlayer1_StatusChange);

    this.axWindowsMediaPlayer1.StatusChange += new EventHandler(serverStartup);
}

public void axWindowsMediaPlayer1_StatusChange(object sender, EventArgs e)
{
    if (axWindowsMediaPlayer1.status == "Finished")
    {
        lblMessage.Text = "CONTINUE PLAYING...";
        axWindowsMediaPlayer1.Ctlcontrols.play();
    }
}

public void serverStartup(object sender, EventArgs e)
{
    FleckLog.Level = LogLevel.Debug;
    var allSockets = new List<IWebSocketConnection>();
    var server = new WebSocketServer("ws://localhost:8181");

    server.Start(socket =>
    {
        socket.OnOpen = () =>
        {
            allSockets.Add(socket);
        };

        socket.OnClose = () =>
        {                     
            allSockets.Remove(socket);
        };

        socket.OnMessage = message =>
        {
            allSockets.ToList().ForEach(s => s.Send("Echo: " + message));    
        };
    });

    if (axWindowsMediaPlayer1.status == "Finished")
    {
        var message_back = "Audio still Playing...";
        foreach (var socket in allSockets.ToList())
        {
            socket.Send(message_back);
        }

        // I tried using this line bellow, but it won't work either.
        //allSockets.ToList().ForEach(s => s.Send("Echo: " + message_back));
    }
}

1 个答案:

答案 0 :(得分:1)

我猜这里有很多,但假设您正在调用axWindowsMediaPlayer1_StatusChange - 在那里添加您的邮件发件人:

// made this global for simplicity...
static List<IWebSocketConnection> allSockets;
allSockets = new List<IWebSocketConnection>();

public void axWindowsMediaPlayer1_StatusChange(object sender, EventArgs e)
{
    if (axWindowsMediaPlayer1.status == "Finished")
    {
        // send message...
        var message_back = "Starting again...";
        foreach (var socket in allSockets.ToList())
        {
            socket.Send(message_back);
        }

        // I assume this restarts playback...
        lblMessage.Text = "CONTINUE PLAYING...";
        axWindowsMediaPlayer1.Ctlcontrols.play();
    }

}