> ## Documentation Index
> Fetch the complete documentation index at: https://devdocs.xbox.com/llms.txt
> Use this file to discover all available pages before exploring further.

# XGameStreamingRegisterConnectionStateChanged

> XGameStreamingRegisterConnectionStateChanged

# XGameStreamingRegisterConnectionStateChanged

ストリーミング クライアント デバイスの接続状態が変化した際に呼び出されるコールバックを登録します。登録時に、既に接続されているすべてのストリーミング クライアント デバイスに対してもコールバックが呼び出されます。

## Syntax

```cpp theme={null}
HRESULT XGameStreamingRegisterConnectionStateChanged(  
         XTaskQueueHandle queue,  
         void* context,  
         XGameStreamingConnectionStateChangedCallback* callback,  
         XTaskQueueRegistrationToken* token  
)  
```

### Parameters

*queue*   \_In\_opt\_\
型: XTaskQueueHandle

変更コールバックを追加する非同期キューへのハンドル。

*context*   \_In\_opt\_\
型: void\*

コールバック関数に渡されるコンテキストへの任意のポインター。

*callback*   \_In\_\
型: XGameStreamingConnectionStateChangedCallback\*

いずれかのストリーミング クライアント デバイスの接続状態が変化した際に呼び出されるコールバック関数。

*token*   \_Out\_\
型: XTaskQueueRegistrationToken\*

イベントの登録解除に使用できる登録トークンへのポインター。

### Return value

型: HRESULT

成功した場合は **S\_OK** を返します。それ以外の場合はエラー コードを返します。

#### 想定されるエラー

| エラー コード                            | エラー値       | エラーの理由                                                                                                                                                     |
| ---------------------------------- | ---------- | ---------------------------------------------------------------------------------------------------------------------------------------------------------- |
| E\_GAMESTREAMING\_NOT\_INITIALIZED | 0x89245400 | XGameStreaming ランタイムが初期化されていません。他の API を呼び出す前に [XGameStreamingInitialize](/reference/system/xgamestreaming/functions/xgamestreaminginitialize) を呼び出してください。 |

エラー コードの一覧については、[Error Codes](/reference/errorcodes) を参照してください。

## Remarks

<Note>この関数は、時間制約のあるスレッドで呼び出すには安全ではありません。詳細については、[時間制約のあるスレッド](https://learn.microsoft.com/gaming/gdk/docs/gdk-dev/console-dev/overviews/threads/time-sensitive-threads)を参照してください。</Note>

この API は、ゲームがストリーミング クライアント デバイスごとにカスタマイズを行い、接続されているデバイスがあるかどうかに応じてゲームがエクスペリエンスを最適化できる状態を維持するための推奨される方法です。

<Note>登録時に、現在接続されているすべてのストリーミング クライアント デバイスに対してコールバックが呼び出されます。</Note>

## Example

```cpp theme={null}
    // Called during game startup to hook into any game streaming start/stop events
    void SetupStreamAware()
    {
        // Register a callback for client connections and disconnections. Note that the connection callback
        // will fire for any already connected clients, so do all of our per-client setup in that callback.      
        XGameStreamingConnectionStateChangedRegistrationToken stateChangeToken = {0};
        XGameStreamingRegisterConnectionStateChanged(
            m_taskQueue, this, ConnectionStateChangedCallback, &stateChangeToken);
    }

    static void ConnectionStateChangedCallback(void* context, XGameStreamingClientId client, XGameStreamingConnectionState state)
    {
        static_cast<Game*>(context)->OnConnectionStateChanged(client, state);
    }

    void OnConnectionStateChanged(XGameStreamingClientId client, XGameStreamingConnectionState state)
    {
        switch (state)
        {
        case XGameStreamingConnectionState::Connected: 
            OnClientConnected(client); 
            break;
        case XGameStreamingConnectionState::Disconnected: 
            OnClientDisconnected(client); 
            break;
        default: 
            break;
        }
    }


    void OnClientConnected(XGameStreamingClientId client)
    {
        // Update list of connected clients
        m_streamingClients.push_back(client);

        // Do per-client initialization
        // ...
    }

    void OnClientDisconnected(XGameStreamingClientId client)
    {
        // Release our reference on the client that is no longer streaming the game
        std::remove(m_streamingClients.begin(), m_streamingClients.end(), client);

        // Do per-client cleanup
        // ...
    }



```

## Requirements

**ヘッダー:** xgamestreaming.h

**ライブラリ:** xgameruntime.lib

**サポートされているプラットフォーム:** Windows、XBOX One ファミリー本体および XBOX Series 本体

## Conceptual documentation

* [Cloud Aware API calls](/build/core-features/common/game-streaming/game-streaming-am-i-streaming)
* [時間制約のあるスレッド](https://learn.microsoft.com/gaming/gdk/docs/gdk-dev/console-dev/overviews/threads/time-sensitive-threads)

## See also

[XGameStreamingConnectionState](/reference/system/xgamestreaming/enums/xgamestreamingconnectionstate)\
[XGameStreamingConnectionStateChangedCallback](/reference/system/xgamestreaming/functions/xgamestreamingconnectionstatechangedcallback)\
[XGameStreamingUnregisterConnectionStateChanged](/reference/system/xgamestreaming/functions/xgamestreamingunregisterconnectionstatechanged)\
[XGameStreaming](/reference/system/xgamestreaming/xgamestreaming_members)


## Related topics

- [XGameStreamingUnregisterConnectionStateChanged](/ja-jp/reference/system/xgamestreaming/functions/xgamestreamingunregisterconnectionstatechanged.md)
- [XGameStreamingConnectionStateChangedCallback](/ja-jp/reference/system/xgamestreaming/functions/xgamestreamingconnectionstatechangedcallback.md)
- [XGameStreamingConnectionState](/ja-jp/reference/system/xgamestreaming/enums/xgamestreamingconnectionstate.md)
- [XGameStreamingIsStreaming](/ja-jp/reference/system/xgamestreaming/functions/xgamestreamingisstreaming.md)
- [XGameStreamingGetConnectionState](/ja-jp/reference/system/xgamestreaming/functions/xgamestreaminggetconnectionstate.md)
