gpt4 book ai didi

C# socket.io 不监听发出事件

转载 作者:太空宇宙 更新时间:2023-11-03 14:59:09 25 4
gpt4 key购买 nike

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

我已经编写了控制面板和 Node.JS 服务器,但是,尽管连接到服务器正常(我知道这是因为我的服务器登录到用户已连接的控制台)。

我是 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);

最佳答案

修复了! :)

在进行最终测试之前我做了很多事情,因此我无法准确指出问题所在。

首先,我更新了 socket.io(使用的是旧项目,我已经将其改编为服务器)。

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

关于C# socket.io 不监听发出事件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47136544/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com