C#socket.io没有监听emit事件

时间:2017-11-06 11:58:24

标签: c# node.js socket.io

我正在尝试编写一个程序,允许我通过控制面板通过websocket(在Raspberry Pi上)发送消息来控制计算机的数量。

我已经编写了控制面板和Node.JS服务器,但是,我遇到了控制台应用程序的问题(加载到我想要控制的PC上),尽管连接到了服务器确定(我知道这是因为我的服务器登录到用户已连接的控制台)。

我是C#的新手(截至昨天),因此我对该语言的了解使我很难找到问题所在。有什么建议吗?

编辑 - .On方法根本没有运行。我试图写入控制台而不是改变音量,但它不起作用。

控制台应用程序:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;


using System.Runtime.InteropServices;
using AudioSwitcher.AudioApi.CoreAudio;
using Quobject.SocketIoClientDotNet.Client;

namespace SoundControlV1
{
    class Program
    {
        //Hide Console Code.
        [DllImport("kernel32.dll")]
        static extern IntPtr GetConsoleWindow();

        [DllImport("user32.dll")]
        static extern bool ShowWindow(IntPtr hWnd, int nCmdShow);

        const int SW_HIDE = 0;
        const int SW_SHOW = 5;


        static void Main(string[] args)
        {
            //Launch Message
            Console.WriteLine("Sound Control V1 Launching...");

            //Init Socket.
            var socket = IO.Socket("http://ip:3000");

            CoreAudioDevice defaultPlaybackDevice = new CoreAudioController().DefaultPlaybackDevice;

            //Sleep.
            System.Threading.Thread.Sleep(2000);

            //Hide Console.
            ShowWindow(GetConsoleWindow(), SW_HIDE);

            Console.WriteLine("Test");
            //Socket Actions.
            socket.On("SOUND_UP", (data) =>
            {
                Console.Write("Test");
                defaultPlaybackDevice.Volume = defaultPlaybackDevice.Volume + 10;
            });

            socket.On("SOUND_DOWN", (data) =>
            {
                defaultPlaybackDevice.Volume = defaultPlaybackDevice.Volume - 10;
            });

            //Prevent app from closing.
            Console.ReadKey();
        }
    }
}

服务器代码:

    var server = require('http').createServer();

var io = require('socket.io')(server);

io.on('connection', function(client){
    //General Functions.
    console.log('A User has connected to the Socket!');
    client.on('disconnect', function(){
        console.log('A User has disconnected from the Socket!');
    });

    //Message Test
    client.on("message", function(data){
       console.log(data);
       client.send(data);
    });

    //Sound Control Functions.
    client.on("SOUND_UP", function(data){
        console.log("SOUND_UP");
        client.emit("SOUND_UP");
    });

    client.on("SOUND_DOWN", function(data){
        console.log("SOUND_DOWN");
        client.emit("SOUND_DOWN");
    });
});
server.listen(3000);

1 个答案:

答案 0 :(得分:0)

修正了它! :)

在进行最后的测试之前,我无法准确确定问题是什么,因为我做了很多事情。

首先,我更新了socket.io(正在使用的那个是旧的,我已经适应了服务器的旧项目)。

其次,我将服务器更改为使用io.emit("SOUND_UP")而不是client.emit...

相关问题