# 自定义服务

本示例演示如何创建 dai.RemoteConnection 并注册自定义的 Visualizer 服务回调。它将启动一个简单的命名服务（myService），当远程调用时返回固定响应。

这个示例需要DepthAI v3 API，参见[安装说明](https://docs.luxonis.com/software-v3/depthai.md)。

## Pipeline

## 源代码

#### Python

```python
#!/usr/bin/env python3
import depthai as dai
import signal
import sys
import time

isRunning = True
def stop():
    global isRunning
    isRunning = False

def signal_handler(sig, frame):
    print('You pressed Ctrl+C!')
    stop()
    sys.exit(0)

signal.signal(signal.SIGINT, signal_handler)

remoteConnection = dai.RemoteConnection()

def testService(input):
    print("Test service called with input:", input)
    return {"result": "testService result"}

remoteConnection.registerService("myService", testService)
while isRunning:
    time.sleep(1)
```

#### C++

```cpp
#include <atomic>
#include <chrono>
#include <csignal>
#include <iostream>
#include <memory>
#include <thread>

#include "depthai/depthai.hpp"
#include "depthai/remote_connection/RemoteConnection.hpp"

// Global flag for graceful shutdown
std::atomic<bool> quitEvent(false);

// Signal handler
void signalHandler(int signum) {
    std::cout << "You pressed Ctrl+C!" << std::endl;
    quitEvent = true;
}

// Test service function
nlohmann::json testService(const nlohmann::json& input) {
    std::cout << "Test service called with input: " << input.dump() << std::endl;
    return {{"result", "testService result"}};
}

int main() {
    // Set up signal handlers
    signal(SIGTERM, signalHandler);
    signal(SIGINT, signalHandler);

    try {
        // Create remote connection
        dai::RemoteConnection remoteConnection;

        // Register service
        remoteConnection.registerService("myService", testService);

        // Main loop
        while(!quitEvent) {
            std::this_thread::sleep_for(std::chrono::seconds(1));
        }

    } catch(const std::exception& e) {
        std::cerr << "Error: " << e.what() << std::endl;
        return 1;
    }

    return 0;
}
```

### 需要帮助？

请前往 [OAKChina 官网](https://www.oakchina.cn/) 获取技术支持或解答您的任何疑问。
