> For the complete documentation index, see [llms.txt](https://www.socketio4j.org/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://www.socketio4j.org/server-api/server-api-4.0-ja/hajimeni/server-instance.md).

# サーバーインスタンス

## サーバーの作成

Socket.IO サーバーを開始するには、次を作成します `Configuration`、バインドアドレスとポートを設定してからサーバーを起動します。

```java
Configuration config = new Configuration();
config.setHostname("localhost");
config.setPort(9092);

SocketIOServer server = new SocketIOServer(config);
```

{% hint style="warning" %}
次のようなフレームワークを使用する場合 **Spring Boot**、次をインポートしていることを確認してください `com.socketio4j.socketio.Configuration` と同名のフレームワーククラス（例えば `org.springframework.context.annotation.Configuration`）ではなく、インポートの競合を避けるためです。
{% endhint %}

## サーバーの起動

{% tabs %}
{% tab title="同期" %}

```java
server.start()
```

{% endtab %}

{% tab title="非同期" %}

```java
server.startAsync().addListener(future -> {
            if (future.isSuccess()) {
                System.out.println("Server started on " + config.getPort());
            } else {
                System.out.println("Error " + future.cause().getLocalizedMessage());
            }
        });
```

{% endtab %}
{% endtabs %}

## サーバーの停止

シャットダウン時は常にサーバーを正常に停止してください。

```java
server.stop();
```

{% hint style="info" %}
socketio4j はサーバー起動後にシャットダウンフックを追加しますが、特定のシナリオでは正常にサーバーを停止しますが、すべてではありません。サーバーを明示的に停止することを常に推奨します。
{% endhint %}

## 完全な例

{% code title="Server.java" %}

```java
import com.socketio4j.socketio.Configuration;
import com.socketio4j.socketio.SocketIOServer;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

public class SocketIoServerMain {
    
    private static final Logger log = LoggerFactory.getLogger(SocketIoServerMain.class);
    
    public static void main(String[] args) throws Exception {
        // 設定を作成
        Configuration config = new Configuration();
        config.setHostname("localhost");
        config.setPort(9092);

        // サーバーを作成
        SocketIOServer server = new SocketIOServer(config);
        
        server.addConnectListener(client -> {
            log.info("[/] connected -> " + client.getSessionId());
        });
        server.addDisconnectListener(client -> {
            log.info("[/] disconnected -> " + client.getSessionId());
        });
        
        server.addEventListener("hi", String.class, (client, data, ack) -> {
            // クライアントで "reply" を監視
            log.info("received data : " + data);
            client.sendEvent("reply", "hello");
        });
        // サーバーを起動
        server.start();
        log.info("Socket.IO server started on port 9092");

        // JVM を維持
        Thread.currentThread().join();
    }
}

```

{% endcode %}

{% hint style="info" %}
確認 [イベント](https://www.socketio4j.org/events/events-ja/) イベント処理に関するドキュメントについては&#x20;
{% endhint %}

## 注意

* `hostname` は省略可能です。設定しない場合、サーバーはすべてのインターフェースにバインドされます（`0.0.0.0` / `::0`).
* `port` **は** サーバーを起動する前に設定する必要があります。
* スレッディング、トランスポート、およびその他の高度なオプションは、次を介してカスタマイズできます `Configuration` を呼び出す前に `start()`.


---

# Agent Instructions
This documentation is published with GitBook. GitBook is the documentation platform designed so that both humans and AI agents can read, navigate, and reason over technical content effectively. Learn more at gitbook.com.

## Querying This Documentation
If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter, and the optional `goal` query parameter:

```
GET https://www.socketio4j.org/server-api/server-api-4.0-ja/hajimeni/server-instance.md?ask=<question>&goal=<endgoal>
```

`ask` is the immediate question: it should be specific, self-contained, and written in natural language.
`goal` is optional and describes the broader end goal you are ultimately trying to accomplish on behalf of the user. GitBook uses it to tailor the answer towards what is most useful for that goal.

The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
