No description
Find a file
obemaru4012 92539ce8b5
Merge pull request #28 from obemaru4012/0.3.3
[0.3.3] README.md修正実施。その2。
2024-12-02 05:03:01 +09:00
bamboo_websocket [0.3.3] postMessageReceivedProc機能とsubProtocolProcess機能を削除。 2024-12-02 04:54:56 +09:00
example [0.3.3] README.md修正実施。 2024-12-02 04:59:46 +09:00
images 【0.2.4】サンプル画像追加 2022-06-13 12:07:16 +09:00
tests [0.3.3] postMessageReceivedProc機能とsubProtocolProcess機能を削除。 2024-12-02 04:54:56 +09:00
.gitignore 【0.3.3】 .gitignore更新実施。 2024-11-25 19:03:54 +09:00
bamboo_websocket.nimble [0.3.3] postMessageReceivedProc機能とsubProtocolProcess機能を削除。 2024-12-02 04:54:56 +09:00
README.md [0.3.3] README.md修正実施。その2。 2024-12-02 05:02:38 +09:00
README_ja.md [0.3.3] README.md修正実施。その2。 2024-12-02 05:02:38 +09:00

🐼Bamboo-WebSocket🌿

FLgp3pCakAAG1JU

  • This is a lightweight WebSocket server implemented entirely in Nim.
  • Our goal is to create a clean and elegant implementation, inspired by the simplicity of splitting bamboo.
  • This project aims to simplify the creation of chat and gaming servers.
  • [TODO] Detailed documentation about Bamboo and its usage will be provided in the wiki.
  • The latest release is 0.3.3.
  • README in Japanese.

🖥Dependency

requires "nim >= 1.4.8"

👩‍💻Setup

nimble install bamboowebsocket@0.3.3

🤔Description

  • It is intended to be used with asynchttpserver, which is provided in the Nim standard.

🤙Usage

🐥Echo Server
  • The following is a server that echoes messages received from clients.
  • The following code can be found in the bamboowebsocket/example/echo_example directory.
# echo_server.nim

import asyncdispatch,
       asynchttpserver,
       asyncnet,
       httpcore,
       nativesockets,
       net,
       strutils,
       uri

from bamboo_websocket/websocket import WebSocket, ConnectionStatus, OpCode
from bamboo_websocket/bamboo_websocket import loadServerSetting, openWebSocket, receiveMessage, sendMessage

proc callBack(request: Request) {.async, gcsafe.} =
  var ws: WebSocket
  var setting = loadServerSetting()

  if request.url.path == "/":
    let headers = {"Content-type": "text/html; charset=utf-8"}
    let content = readFile("./echo_client.html")
    await request.respond(Http200, content, headers.newHttpHeaders())

  if request.url.path == "/chat":
    try:
      ws = await openWebSocket(request, setting)
    except:
      let message = getCurrentException()

    while ws.status == ConnectionStatus.OPEN:
      try:
        let receive = await ws.receiveMessage()

        if receive[0] == OpCode.TEXT:
          echo("ID: ", ws.id, " echo back.")
          await ws.sendMessage(receive[1], 0x1, 3000, true)

        if receive[0] == OpCode.CLOSE:
          echo("ID: ", ws.id, " has Closed.")
          break

      except:
        ws.status = ConnectionStatus.CLOSED
        ws.socket.close()

    ws.socket.close()

if isMainModule:
  var server = newAsyncHttpServer()
  waitFor server.serve(Port(9001), callBack)

  • A json file describing the configuration file for the server must be placed in the same location as the server file (e.g. ehco_server.nim)。
{
  "websocket_version": "13",
  "upgrade": "websocket",
  "connection": "upgrade",
  "websocket_key": "dGhlIHNhbXBsZSBub25jZQ==",
  "magic_strings": "258EAFA5-E914-47DA-95CA-C5AB0DC85B11",
  "mask_key_seeder": "514902776"
}
  • Run echo_server.nim after compilation.
nim c -r echo_server.nim

002

😏Advanced Usage

🐄Chat Server
  • The following code is a server that enables chatting between each client.
  • The following code can be found in the bamboowebsocket/example/chat_example directory.
# chat_server.nim

import asyncdispatch,
       asynchttpserver,
       asyncnet,
       httpcore,
       json,
       nativesockets,
       net,
       strutils,
       tables,
       uri

from bamboo_websocket/websocket import WebSocket, ConnectionStatus, OpCode
from bamboo_websocket/bamboo_websocket import loadServerSetting, openWebSocket, receiveMessage, sendMessage

var WebSockets: seq[WebSocket] = newSeq[WebSocket]()

proc callBack(request: Request) {.async, gcsafe.} =

  var ws = WebSocket()
  var setting = loadServerSetting()

  if request.url.path == "/":
    let headers = {"Content-type": "text/html; charset=utf-8"}
    let content = readFile("./chat_client.html")
    await request.respond(Http200, content, headers.newHttpHeaders())

  if request.url.path == "/chat":
    try:
      ws = await openWebSocket(request, setting)

      # SubProtocol Proc名前を取得、さらにURI decode処理を追加
      var sub_protocol = decodeUrl(request.headers["sec-websocket-protocol", 0])
      ws.optional_data["name"] = $(sub_protocol)

      WebSockets.add(ws)
      echo("ID: ", ws.id, ", Tag: ", ws.optional_data["name"], " has Opened.")
    except:
      let message = getCurrentException()
      echo(message.msg)

      ws.status = ConnectionStatus.INITIAl

    while ws.status == ConnectionStatus.OPEN:
      try:
        let receive = await ws.receiveMessage()
        if receive[0] == OpCode.TEXT:
          var message: string = $(%* [{"name": ws.optional_data["name"], "message": receive[1]}])
          for websocket in WebSockets:
            if websocket.id != ws.id:
              echo("$# => $#" % [$(ws.id), $(websocket.id)])
              await websocket.sendMessage(message, 0x1)

        if receive[0] == OpCode.CLOSE:
          echo("ID: ", ws.id, " has Closed.")
          break

      except:
        ws.status = ConnectionStatus.CLOSED
        ws.socket.close()

    if WebSockets.find(ws) != -1:
      WebSockets.delete(WebSockets.find(ws))

    if ws.status == ConnectionStatus.OPEN:
      ws.socket.close()

if isMainModule:
  var server = newAsyncHttpServer()
  waitFor server.serve(Port(9001), callBack)

  • A json filesetting.json describing the configuration file for the server must be placed in the same location as the server file (e.g. chat_server.nim)。
{
  "websocket_version": "13",
  "upgrade": "websocket",
  "connection": "upgrade",
  "websocket_key": "dGhlIHNhbXBsZSBub25jZQ==",
  "magic_strings": "258EAFA5-E914-47DA-95CA-C5AB0DC85B11",
  "mask_key_seeder": "514902776"
}
  • Run chat_server.nim after compilation.
nim c -r chat_server.nim

004

🐭Game Server

[TODO]

📝Author

📖References