websocket_server

MODULE

websocket_server

MODULE SUMMARY

Server-side implementation for the WebSocket Protocol

DESCRIPTION

The websocket_server module provides functions for communicating using the WebSocket protocol.

The following code fragment provides a simple example of a echo server:

-module(echo_handler).
-compile(export_all).
-import(websocket_server, [unicast/2]).
go() ->
   websocket_server:start("localhost", 9000, ?MODULE, default_echo_handler, []).
default_echo_handler() ->
  receive
    {message, Data, ConnectionID} -> 
      unicast(Data, ConnectionID),
      default_echo_handler();
    _Any -> default_echo_handler()
  end.

DATA TYPES

ip_address()
  see inet(3)

EXPORTS

start() -> void()

Types:

Starts the WebSocket simple echo server. This function calls start("localhost", 9000, ?MODULE, default_echo_handler, []).

start(Host, Port, Module, Handler, Args) -> void()

Types:

Host = ip_address() | string() | atom()
Port = 0..65535
Module = Handler = atom()
Args = [term()]

Starts the WebSocket server on port Port as host Host, handler Module:Handler with args Args.

unicast(Data, ConnectionID) -> void()

Types:

Data = string()
ConnectionID = string()

Send the data Data to the client whose id is ConnectionID.

broadcast(Data, ConnectionID) -> void()

Types:

Data = string()
ConnectionID = string()

Send the data Data to all the clients excluding the one whose id is ConnectionID.

sendall(Data) -> void()

Types:

Data = string()

Send the data Data to all the clients connected.