> For the complete documentation index, see [llms.txt](/llms.txt)

# GS2-Gateway SDK for Game Engine API リファレンス

ゲームエンジン向け GS2-Gateway SDK の モデルの仕様 と API のリファレンス



## モデル

### EzWebSocketSession

WebSocketSession<br>

WebSocketセッションはGS2サーバとクライアント間の持続的な接続で、リアルタイムに双方向通信を行います。<br>
サーバに対してクライアント側から識別子としてユーザーIDを登録します。

|  | 型 | 有効化条件 | 必須 | デフォルト | 値の制限 | 説明 |
| --- | --- | --- | --- | --- | --- | --- |
| connectionId | string |  | ✓ |  |  ~ 128文字 | コネクションID<br>この WebSocket 接続に割り当てられた一意な識別子です。通知送信時に特定のクライアント接続を識別するために使用されます。 |
| namespaceName | string |  | ✓ |  |  ~ 128文字 | ネームスペース名 |
| userId | string |  | ✓ |  |  ~ 128文字 | ユーザーID |

**関連するメソッド:**
setUserId - サーバーからのプッシュ通知を受け取るためにプレイヤーの接続を登録する


---

## メソッド

### setUserId

サーバーからのプッシュ通知を受け取るためにプレイヤーの接続を登録する<br>

現在のWebSocket接続をプレイヤーのユーザーIDに紐づけて、サーバーからこのクライアントにリアルタイムのプッシュ通知を送信できるようにします。<br>
通常はプレイヤーがログインしてサーバーに接続した直後に呼び出します。このステップなしでは、サーバーはどの接続がどのプレイヤーのものか判別できません。<br>
allowConcurrentAccess フラグで、同じプレイヤーが複数のデバイスから同時に接続できるかを制御します：<br>
- true: 複数の同時接続を許可（スマホとタブレットの両方でプレイするなど）<br>
- false: プレイヤーごとに1接続のみ許可（重複ログイン防止。古い接続は切断されます）<br>
ゲームのログイン・初期化フローの一部として使います。リアルタイムチャット通知、フレンドリクエストのアラート、マッチング成立の通知などの機能を有効にするのに必要です。

#### Request

|  | 型 | 有効化条件 | 必須 | デフォルト | 値の制限 | 説明 |
| --- | --- | --- | --- | --- | --- | --- |
| namespaceName | string |  | ✓|  |  ~ 128文字 | ネームスペース名 |
| gameSession | GameSession | | ✓|  |  | GameSession |
| allowConcurrentAccess | bool |  | | true |  | 同時に異なるクライアントからの接続を許容するか |
| sessionId | string | {allowConcurrentAccess} == false | |  |  ~ 128文字 | allowConcurrentAccess を false にした場合でも、既存接続と同一の sessionId であれば接続を許可するために指定します。 |

#### Result

|  | 型 | 説明 |
| --- | --- | --- |
| item | [EzWebSocketSession](#ezwebsocketsession) | 更新したWebSocketセッション|

#### 実装例




**Unity (UniTask)**
```csharp
    var domain = gs2.Gateway.Namespace(
        namespaceName: "$hash"
    ).Me(
        gameSession: GameSession
    ).WebSocketSession(
    );
    var result = await domain.SetUserIdAsync(
        allowConcurrentAccess: true,
        sessionId: null
    );
    var item = await result.ModelAsync();

```

**Unity (Vanilla)**
```cs
    var domain = gs2.Gateway.Namespace(
        namespaceName: "$hash"
    ).Me(
        gameSession: GameSession
    ).WebSocketSession(
    );
    var future = domain.SetUserIdFuture(
        allowConcurrentAccess: true,
        sessionId: null
    );
    yield return future;
    if (future.Error != null)
    {
        onError.Invoke(future.Error, null);
        yield break;
    }
    var future2 = future.Result.ModelFuture();
    yield return future2;
    if (future2.Error != null)
    {
        onError.Invoke(future2.Error, null);
        yield break;
    }
    var result = future2.Result;

```

**Unreal Engine 5**
```cpp
    const auto Domain = Gs2->Gateway->Namespace(
        "$hash" // namespaceName
    )->Me(
        GameSession
    )->WebSocketSession(
    );
    const auto Future = Domain->SetUserId(
        true // allowConcurrentAccess
        // sessionId
    );
    Future->StartSynchronousTask();
    if (Future->GetTask().IsError())
    {
        return false;
    }

    // obtain changed values / result values
    const auto Future2 = Future->GetTask().Result()->Model();
    Future2->StartSynchronousTask();
    if (Future2->GetTask().IsError())
    {
        return Future2->GetTask().Error();
    }
    const auto Result = Future2->GetTask().Result();

```


---



