【问题标题】:How to handle WebSocket messages in an appmod using Yaws?如何使用 Yaws 在 appmod 中处理 WebSocket 消息?
【发布时间】:2012-02-29 13:30:34
【问题描述】:

我创建了一个简单的appmod,它会在收到的消息时发回相同的消息。但是我在命令提示符下收到一条错误消息,并且 WebSocket 连接已关闭。

如果我发送带有 3 个字符的消息,我会收到以下错误消息:

=ERROR REPORT==== 8-Feb-2012::05:09:14 ===
Error in process <0.59.0> with exit value: {undef,[{mywebsocket,handle_message,[
{text,<<3 bytes>>}],[]},{lists,map,2,[{file,"lists.erl"},{line,1173}]},{yaws_web
sockets,loop,4,[{file,"yaws_websockets.erl"},{line,151}]}]}

我做错了什么?句柄使用text,如果我使用binary也不起作用。

这是我的 HTML+JavaScript 客户端:

<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8"/>
<script>
window.onload = function() {
    document.getElementById('sendbutton').addEventListener('click', sendMessage, false);
    document.getElementById('connectbutton').addEventListener('click', connect, false);
    document.getElementById('disconnectbutton').addEventListener('click', disconnect, false);
}

function writeStatus(message) {
    var html = document.createElement("div");
    html.setAttribute("class", "message");
    html.innerHTML = message;
    document.getElementById("status").appendChild(html);
}

function connect() {
    ws = new WebSocket("ws://localhost:8090/ws.yaws");

    ws.onopen = function(evt) {
        writeStatus("connected");
    }

    ws.onclose = function(evt) {
        writeStatus("disconnected");
    }

    ws.onmessage = function(evt) {
        writeStatus("response: " + evt.data);
    }

    ws.onerror = function(evt) {
        writeStatus("error: " + evt.data);
    }
}

function disconnect() {
    ws.close();
}

function sendMessage() {
    var msg = document.getElementById('messagefield').value
    ws.send(msg);
}
</script>
</head>
<body>
<h1>Chat</h1>
<button id="connectbutton">Connect</button>
<button id="disconnectbutton">Disconnect</button><br/>
<input type="text" id="messagefield"/><button id="sendbutton">Send</button>
<div id="status"></div>
</body>
</html>

我的ws.yaws 从客户端调用连接 看起来像这样:

<erl>
out(A) -> {websocket, mywebsocket, []}.
</erl>

我的回调 appmod mywebsocket.erl 看起来像这样:

-module(mywebsocket).
-export([handle_message/1]).

handle_message({text, Message}) ->
    {reply, {text, Message}}.

yaws.conf我已经这样配置了服务器:

<server localhost>
    port = 8090
    listen = 0.0.0.0
    docroot = "C:\Users\Jonas/yawswww"
    appmods = <ws, mywebsocket>
</server>

我使用 Yaws 1.92Chrome 16

【问题讨论】:

    标签: erlang websocket yaws


    【解决方案1】:

    很可能,您的 appmod 不在 PATH 中,或者它的模块未加载到 yaws Web 服务器 VM 实例或 shell 中。一旦你的网络服务器启动,尝试在 yaws shell 中调用这个方法:

    1> mywebsocket:handle_message({text,"一些数据"})。 如果它在 yaws shell 中运行得很好,那么这意味着它在 PATH 中。错误是undef,这意味着函数调用失败,因为包含该函数的模块未加载。

    现在,在 yaws.conf 文件中,编辑它以包含您的 appmod 的已编译文件所在的 ebin 文件夹。实际上,请确保将所有 PATHS 添加到您的 appmod 所依赖的可执行代码中。它应该是这样的:

    # This the path to a directory where additional
    # beam code can be placed. The daemon will add this
    # directory to its search path
    
    ebin_dir = "F:/programming work/erlcharts-1.0/ebin"
    ebin_dir = "C:/SomeFolder/another_library-1.0/ebin"
    ebin_dir = "D:/Any/Folder/myappmods"
    
    # This is a directory where application specific .hrl
    # files can be placed. application specifig .yaws code can
    # then include these .hrl files
    
    include_dir = "C:\Program Files (x86)\Yaws-1.90/examples/include"
    include_dir = "D:/Any/Folder/myappmods/include"
    

    现在,在 yaws conf 文件中输入所有代码的路径。不要担心正斜杠或反斜杠,偏航总是绕过路径。对于 UNIX/LINUX,它保持不变,例如ebin_dir = "/usr/local/lib/erlang/lib/myapp-1.0/ebin".

    但是,请注意,在 yaws Web 服务器完全初始化之前,您的模块不会被加载

    【讨论】:

    • 谢谢,这行得通。我还将您其他帖子中的内容添加到此答案中。你应该编辑你的答案而不是发布新的答案。
    猜你喜欢
    • 2021-12-06
    • 1970-01-01
    • 1970-01-01
    • 2012-03-02
    • 2021-05-29
    • 1970-01-01
    • 2013-03-19
    • 2019-01-13
    • 2019-03-04
    相关资源
    最近更新 更多