Erlang 和 websocket

发布于 2024-10-04 11:29:11 字数 203 浏览 3 评论 0原文

前段时间我在 Erlang 和 websocket 上发现了 Joe Armstrong 的示例,但我无法让它工作。

我修复了 Erlang 代码中的一个错误和几个警告,但使用 Apache 时我无法获得良好的结果。

有人可以用一个非常简单的例子给我一些提示吗?我是否需要像普通 PHP 文件一样将带有 JavaScript 的网页放入 Apache 目录中?

Some time ago I found Joe Armstrong's example on Erlang and websocket, but I couldn't get it work.

I fixed an error and a couple of warnings in the Erlang code but with Apache I wasn't able to get a good result.

Can anybody give me some hints with a really simple example? Do I need to put the web page with the JavaScript inside the Apache directory as for normal PHP files?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

写下不归期 2024-10-11 11:29:11

Joe 的 websocket 示例已经过时,并且依赖于过时的协议版本。最新的浏览器使用更新的版本 (draft-00)

截至今天,misultin 提供了一个很好的 erlang 实现。经过测试并与当前浏览器兼容:

https://github.com/ostinelli/ Misultin/blob/master/src/misultin_websocket.erl

Joe's websocket example is outdated and relies on an obsolete version of the protocol. Up-to-date browsers use a more recent version (draft-00)

As of today, a nice erlang implementation is available from misultin. Tested and compatible with current browsers:

https://github.com/ostinelli/misultin/blob/master/src/misultin_websocket.erl

篱下浅笙歌 2024-10-11 11:29:11

Yaws Web 服务器提供了 Websocket 实现。我还写了一个行为来简化基于ws的应用程序的编写。它是我的 Erlang 工具的一部分(实际上是第一个):

https://github.com/ schemaway/erlang-tools

The Yaws webserver provides a websocket implementation. I also wrote a behaviour to simplify the writing of ws-based applications. It's part of my Erlang tools (well, the first one in fact):

https://github.com/schemeway/erlang-tools

烟沫凡尘 2024-10-11 11:29:11

我发现 SockJS-Erlang 库工作得非常好。最重要的是,如果 Websocket 不可用,它支持后备传输。它使用 Cowboy (虽然是旧版本)作为底层服务器,这很好,因为它很容易集成。 此脚本此 HTML 页面 将为您提供一个可以使用的演示。

这里是一个带注释的示例:

start_link(_) ->
    application:start(sockjs),
    application:start(cowboy),

    % generate a SockJS handler
    SockjsState = sockjs_handler:init_state(
                    <<"/browser_socket">>, fun handle_client/3, state, []),

    % build the dispatch routes for Cowboy integrating the SockJS handler
    Routes = [{'_',  [{[<<"echo">>, '...'],
                       sockjs_cowboy_handler, SockjsState}]}],

    % start the cowboy server
    cowboy:start_listener(http, 100,
                          cowboy_tcp_transport, [{port,     8081}],
                          cowboy_http_protocol, [{dispatch, Routes}]),

% called when a new client connects
handle_client(Conn, init, state) -> {ok, state};

% called when data is received 
handle_client(Conn, {recv, Data}, state) ->
  % reply to client
  Conn:send(Data);

% called when connection is closed
handle_client(_Conn, closed, state) -> {ok, state}.

我对 Apache 的建议是使用 HAProxy 进行 WebSocket 连接,使用 Apache 提供 Javascript 和 PHP 服务。

I found the SockJS-Erlang library to work wonderfully well. Best of all it supports fallback transports if websockets are not available. It uses Cowboy (although an older version) as the underlying server which is nice because it is easy to integrate with. This escript and this HTML page will give you a working demo you can play around with.

here is an annotated example:

start_link(_) ->
    application:start(sockjs),
    application:start(cowboy),

    % generate a SockJS handler
    SockjsState = sockjs_handler:init_state(
                    <<"/browser_socket">>, fun handle_client/3, state, []),

    % build the dispatch routes for Cowboy integrating the SockJS handler
    Routes = [{'_',  [{[<<"echo">>, '...'],
                       sockjs_cowboy_handler, SockjsState}]}],

    % start the cowboy server
    cowboy:start_listener(http, 100,
                          cowboy_tcp_transport, [{port,     8081}],
                          cowboy_http_protocol, [{dispatch, Routes}]),

% called when a new client connects
handle_client(Conn, init, state) -> {ok, state};

% called when data is received 
handle_client(Conn, {recv, Data}, state) ->
  % reply to client
  Conn:send(Data);

% called when connection is closed
handle_client(_Conn, closed, state) -> {ok, state}.

My advice regarding Apache would be to use HAProxy for your WebSocket connections and Apache for serving Javascript and PHP.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文