erlang用cowboy搭建简单服务器收发http

Cowboy

cowboy指南:Nine Nines: Cowboy User Guide

Cowboy是一个小型、快速和现代化的Erlang/OTP HTTP服务器,这意味着我们可以很方便轻松地通过Cowboy来实现一个http服务器。

rebar和cowboy的教学后续更新

开始教学!

创建项目

准备内容

首先,我们需要准备开发ide(这里用了idea作为开发ide)、Erlang/OTP(这里用的OTP25.3)、rebar3(以及windows环境下会用到的rebar3.cmd)

附上部分内容下载地址:

erlang:Downloads - Erlang/OTP

rebar3:https://github.com/erlang/rebar3/archive/refs/heads/main.zip

rebar3需要依赖于erlang,所以需要先安装erlang并设置环境变量后再跑rebar3,windows情况下双击bootstrap.bat运行后就会在文件夹内生成rebar3和rebar3.cmd了

新建app

将rebar3和rebar3.cmd放到工作目录中,打开cmd在目录里执行rebar3 new app $app_name (这里的$app_name是自己定的,我这边用了cowboy_test,所以后续都用cowboy_test表示app名字),

会发现生成了以自己设定的名字生成的文件夹,里面有一个rebar.config文件和一些其他文件,接下来我们把rebar3和rebar3.cmd移入文件夹内方便后续运行

idea用户可以顺便新建一下工作目录:File -> New -> Project from Existing Source,选择生成的文件夹,会自动勾选Rebar model,点击next后可以设置rebar路径,设置为rebar3所在的路径,之后一路next就可以啦

这时候我们就得到了一个全新的rebar构建的app

rebar配置

因为我们需要用到cowboy,所以我们需要将cowboy加入到依赖项中,共有两个地方需要添加:

rebar.config文件:在{deps, []}.的中括号里加上{cowboy, “2.10.0”},表示rebar需要下载cowboy依赖,也就是这一行变成:{deps, [{cowboy, “2.10.0”}]}. 后续有新增依赖的时候也是在列表中继续以元组的形式增加依赖

src/cowboy_test.app.src文件:在{applications, [kernel,stdlib]},的后面加上cowboy,表示app内会用到cowboy依赖,也就是这一行变成:{applications, [kernel,stdlib,cowboy]},

配置加好之后,我们需要编译下载添加的依赖:

没有idea:我们在工作目录中打开cmd,运行rebar3 compile,就可以下载依赖

有idea:在Run -> Configurations里新加一个Erlang Rebar命令,command只需要compile,保存之后点击run即可完成编译

如果编译完成之后看到一个cowboy_test/_build文件夹,说明编译完成

cowboy启动

cowboy里最常用到的三个方法:

cowboy_router:compile/1(用于编译访问映射)

cowboy:start_clear/3(用于监听指定端口)

cowboy_req:reply/4(用于发送消息)

在项目启动时会执行cowboy_test_app:start/2,所以我们在这里加一个开启cowboy监听的函数start_cowboy/0

-module(cowboy_test_app).

start(_StartType, _StartArgs) ->
    start_cowboy(),
    cowboy_test_sup:start_link().

start_cowboy() ->
    Dispatch = cowboy_router:compile([
        {'_', [{"/my_cowboy", my_cowboy, []}]}	%% 访问映射,例如这里是http://localhost:26521/mycowboy  访问里面内容
    ]),
    {ok, _} = cowboy:start_clear(cowboy_http_listener,
        [{port, 26521}],						%% 访问端口
        #{env => #{dispatch => Dispatch}}		%% 映射
    ).

在这里我们定义了一个my_cowboy的模块处理/my_cowboy路径下请求的信息,所以我们需要新建一个my_cowboy.erl的模块,并添加一个init/2来处理请求信息:

-module(my_cowboy).
-export([init/2]).

init(Req, State) ->
    Req2 = cowboy_req:reply(200, #{<<"content-type">> => <<"text/plain">>}, <<"Hello Cowboy">>, Req),
    {ok, Req2, State}.

这里的操作就是当http请求当前路径时,返回一个200的http请求码,同时发送一个“Hello Cowboy”字符串

代码写完之后先编译,然后执行rebar3 shell即可在shell里启动应用了,此时打开浏览器访问http://localhost:26521/my_cowboy即可向26521端口发送请求并得到返回。

有idea的情况下可以新加rebar的shell命令,然后在before launch里添加一个Run Another Configuration并选择之前加的compile,即可在运行shell的时候一键编译运行


至此服务器已经可以通过rebar shell运行了,通过上面的coding已经可以作为正常收发的一个服务器底子了,有不同需求可以通过不同路径和对应模块处理并返回,这里不做额外coding

但是一堆代码并不适合移植到别的电脑运行,这时候我们就开始进行版本发布并打包,将运行所需内容全部打包起来:

发布

rebar发布需要在rebar.config设置发布相关配置,如下:

{relx, [{release, {<release name>, "0.0.1"},
         [<app_name>]},

        {release, {<other release name>, "0.1.0"},
         [<app_name>]},

         {dev_mode, false},
         {include_erts, true},
       ]}.

可以通过不同的release name对同一个app设置不同的版本(其中app_name是对应app_name.app.src文件的)

我在项目中使用的配置如下:

{relx, [{release, {cowboy_test, "0.1.0"},
    [cowboy_test]},
    {dev_mode, false},
    {include_erts, true},	% 设置自带erlang运行环境,自带的话包会大一些
    {include_src, false},	% 设置不包含源文件
    {extended_start_script, true}
]
}.

之后运行rebar3 release,会在_build/default下找到一个rel文件夹,这里面的就是发布后的文件,其中运行cmd在bin文件夹内,需要运行发布版本时,可以运行bin目录下的cmd文件。(help可以查看可选参数)

D:\cowboy_test_build\default\rel\cowboy_test\bin> cowboy_test.cmd help
Usage: cowboy_test (install|uninstall|start|stop|restart|upgrade|downgrade|console|ping|list|attach|remote_console|undefined)

接下来的内容需要打开管理员模式,如果运行出现诸如:

Unable to register cowboy_test_0.1.0 service with service manager.

这样的提示,说明没有开启管理员模式

安装流程:

首先需要通过cowboy_test install 安装服务,当出现 Service cowboy_test_0.1.0 added to system.这个消息的时候说明安装完成,接下来可以有两种启动方式:

cowboy_test start (后台启动,此时关闭shell不会停止服务,直到执行了cowboy_test stop才会停止)

cowboy_test console (控制台启动,当shell被关闭时服务同时停止)

卸载执行cowboy_test uninstall 即可

打包

我们已经将应用发布成cmd的命令行可运行格式了,接下来我们可以通过命令rebar3 tar将这些所需要的文件打包成一个压缩包,方便部署到其他机器上,当命令执行完毕后我们可以在返回的目录中找到对应的压缩包文件(压缩包内容和发布出来的内容是一样的)

部署的时候解压然后同安装流程即可

好的,以下是一个简单的使用erlangcowboy框架编写的websocket聊天室代码示例。 ```erlang %% 定义路由和处理函数 -module(chat_router). -behaviour(cowboy_router). -export([init/3]). -export([routes/2]). init(_Transport, _Req, _Opts) -> {ok, [], undefined}. routes(_Req, _State) -> {[ {'_', [ {"/websocket", cowboy_websocket_handler, [ {callback, chat_handler}, {origin, "*"} ]} ]} ], _Req, _State}. %% 定义websocket处理函数 -module(chat_handler). -behaviour(cowboy_websocket_handler). -export([init/3]). -export([websocket_init/3]). -export([websocket_handle/3]). -export([websocket_info/3]). -export([websocket_terminate/3]). init(_TransportName, Req, _Opts) -> {ok, Req, undefined}. websocket_init(_TransportName, Req, _Opts) -> {cowboy_websocket, Req, undefined}. websocket_handle({text, Message}, Req, _State) -> %% 处理收到的文本消息 cowboy_websocket:broadcast(Message, Req), {ok, Req, undefined}; websocket_handle(_Data, Req, _State) -> {ok, Req, undefined}. websocket_info({join, _Pid, _Ref}, Req, _State) -> %% 处理新用户加入聊天室 cowboy_websocket:broadcast("New user joined", Req), {ok, Req, undefined}; websocket_info({leave, _Pid, _Ref}, Req, _State) -> %% 处理用户离开聊天室 cowboy_websocket:broadcast("User left", Req), {ok, Req, undefined}; websocket_info(_Info, Req, _State) -> {ok, Req, undefined}. websocket_terminate(_Reason, Req, _State) -> {ok, Req, undefined}. ``` 以上代码定义了一个路由模块`chat_router`,其中`routes/2`函数指定了处理`/websocket`路径的`cowboy_websocket_handler`处理函数,并将其回调函数指定为`chat_handler`。`chat_handler`模块中的`websocket_handle/3`函数用于处理接收到的文本消息,并使用`cowboy_websocket:broadcast/2`函数将消息广播给所有连接的客户端。`websocket_info/3`函数用于处理新用户加入和离开聊天室的事件,同样使用`cowboy_websocket:broadcast/2`函数将事件广播给所有连接的客户端。 您可以将以上代码保存为`chat.erl`文件,并使用cowboy框架启动websocket服务器,例如: ```erlang %% 启动websocket服务器 -module(chat). -behaviour(application). -export([start/2, stop/1]). start(_Type, _Args) -> Dispatch = cowboy_router:compile([ {'_', [{"/websocket", chat_handler, []}]} ]), {ok, _} = cowboy:start_clear(http, [{port, 8080}], #{env => #{dispatch => Dispatch}}), chat_sup:start_link(). stop(_State) -> ok. ``` 以上代码定义了一个应用模块`chat`,其中`start/2`函数启动了一个cowboy服务器,监听8080端口,并将路由配置为处理`/websocket`路径,使用`chat_handler`处理函数。您可以将以上代码保存为`chat_app.erl`文件,并使用`rebar3`编译和启动应用程序: ```sh $ rebar3 compile $ rebar3 shell ``` 在erlang shell中,您可以使用以下命令连接到websocket服务器并发送消息: ```erlang %% 连接到websocket服务器 1> {ok, Conn} = websocket:connect("ws://localhost:8080/websocket"). {ok,{websocket,#Port<0.7>,<0.198.0>}} %% 发送消息 2> websocket:send_text(Conn, "Hello, World!"). ok ``` 在另一个erlang shell中,您可以使用以下命令连接到相同的websocket服务器并接收消息: ```erlang %% 连接到websocket服务器 1> {ok, Conn} = websocket:connect("ws://localhost:8080/websocket"). {ok,{websocket,#Port<0.7>,<0.203.0>}} %% 接收消息 2> websocket:recv_text(Conn). "New user joined" 3> websocket:recv_text(Conn). "Hello, World!" ```
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值