2012-02-08 81 views
2

我创建了一个简单的appmod,它发回的消息与接收到的消息相同。但是,我在命令提示符下收到错误消息,并且WebSocket连接已关闭。如何使用Yaws处理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> 

而且我的回调appmodmywebsocket.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> 

我使用雅司1.92铬16

回答

2

也许,您的appmod不在PATH中,或者其模块未加载到雅虎Web服务器VM实例或外壳中。尝试在web服务器启动后在yaws shell中调用此方法:

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


现在,在yaws.conf文件,对其进行编辑,包括您的appmod的编译文件所在的文件夹EBIN。实际上,确保将所有PATH添加到您的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" 

现在,输入PATH到您在yaws conf文件中的所有代码。不要担心正斜杠或反斜杠,雅各布总是绕着路径前进。对于UNIX/LINUX,它仍然是一样的。 ebin_dir = "/usr/local/lib/erlang/lib/myapp-1.0/ebin"

但是请注意,你的模块将不会被尚未加载,直到雅司病web服务器完全初始化

+1

谢谢,这个工作。我还将您其他帖子的内容添加到了此答案中。你应该编辑你的答案,而不是发布新的答案。 – Jonas 2012-02-08 17:40:59