I have created a simple appmod that sends back the same message as it receives. But I get an error message at the command prompt and the WebSocket connection is closed.
If I send a message with 3 chars I get this error message:
=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}]}]}
What am I doing wrong? The handle uses text
, and it doesn't work if I use binary
either.
Here is my HTML+JavaScript client:
<!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>
My ws.yaws
that is called on connection from the client looks like this:
<erl>
out(A) -> {websocket, mywebsocket, []}.
</erl>
And my callback appmod mywebsocket.erl
looks like this:
-module(mywebsocket).
-export([handle_message/1]).
handle_message({text, Message}) ->
{reply, {text, Message}}.
In yaws.conf
I have configured the server like this:
<server localhost>
port = 8090
listen = 0.0.0.0
docroot = "C:\Users\Jonas/yawswww"
appmods = <ws, mywebsocket>
</server>
I use Yaws 1.92 and Chrome 16.