2

我创建了一个简单的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

4

1 回答 1

2

可能,您的 appmod 不在 PATH 中,或者它的模块未加载到 yaws Web 服务器 VM 实例或 shell 中。一旦您的 Web 服务器启动,请尝试在 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 服务器完全初始化之前,您的模块不会被加载

于 2012-02-08T12:18:30.803 回答