-
Notifications
You must be signed in to change notification settings - Fork 335
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
7 changed files
with
212 additions
and
23 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,43 @@ | ||
package org.scalatra | ||
|
||
import socketio.SocketIOSupport | ||
import com.glines.socketio.server.SocketIOFrame | ||
|
||
class ChatServlet extends ScalatraServlet with SocketIOSupport { | ||
|
||
|
||
socketio { socket => | ||
socket.onConnect { connection => | ||
println("Connecting chat client [%s]" format connection.clientId) | ||
try { | ||
connection.send(SocketIOFrame.JSON_MESSAGE_TYPE, """{ "welcome": "Welcome to Socket IO chat" }""") | ||
} catch { | ||
case _ => connection.disconnect | ||
} | ||
connection.broadcast(SocketIOFrame.JSON_MESSAGE_TYPE, | ||
"""{ "announcement": "New participant [%s]" }""".format(connection.clientId)) | ||
} | ||
|
||
socket.onDisconnect { (connection, reason, _) => | ||
println("Disconnecting chat client [%s] (%s)".format(connection.clientId, reason)) | ||
connection.broadcast(SocketIOFrame.JSON_MESSAGE_TYPE, | ||
"""{ "announcement": "Participant [%s] left" }""".format(connection.clientId)) | ||
} | ||
|
||
socket.onMessage { (connection, _, message) => | ||
println("RECV: [%s]" format message) | ||
message match { | ||
case "/rclose" => { | ||
connection.close | ||
} | ||
case "/rdisconnect" => { | ||
connection.disconnect | ||
} | ||
case _ => { | ||
connection.broadcast(SocketIOFrame.JSON_MESSAGE_TYPE, | ||
"""{ "message": ["%s", "%s"] }""".format(connection.clientId, message)) | ||
} | ||
} | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,105 @@ | ||
<!doctype html> | ||
<html> | ||
<head> | ||
<title>socket.io client test</title> | ||
|
||
<script src="/js/json.js"></script> <!-- for ie --> | ||
<script src="/socket.io/socket.io.js"></script> | ||
</head> | ||
<body> | ||
|
||
<script> | ||
function addLine(txt) { | ||
var el = document.createElement('p'); | ||
el.innerHTML = txt; | ||
document.getElementById('chat').appendChild(el); | ||
document.getElementById('chat').scrollTop = 1000000; | ||
} | ||
function message(obj){ | ||
if ('welcome' in obj) addLine('<em>' + esc(obj.welcome) + '</em>'); | ||
else if ('announcement' in obj) addLine('<em>' + esc(obj.announcement) + '</em>'); | ||
else if ('message' in obj) addLine('<b>' + esc(obj.message[0]) + ':</b> ' + esc(obj.message[1])); | ||
} | ||
|
||
function send(){ | ||
var val = document.getElementById('text').value; | ||
if (val == '/lclose') { | ||
addLine('<em>closing...</em>'); | ||
socket.close(); | ||
} else if (val == '/ldisconnect') { | ||
addLine('<em>disconnecting...</em>'); | ||
socket.disconnect(); | ||
} else { | ||
socket.send(val); | ||
} | ||
message({ message: ['you', val] }); | ||
document.getElementById('text').value = ''; | ||
} | ||
|
||
function esc(msg){ | ||
return msg.replace(/</g, '<').replace(/>/g, '>'); | ||
}; | ||
|
||
function drname(dr) { | ||
if (dr == 1) { | ||
return 'CONNECT_FAILED'; | ||
} else if (dr == 2) { | ||
return 'DISCONNECT'; | ||
} else if (dr == 3) { | ||
return 'TIMEOUT'; | ||
} else if (dr == 4) { | ||
return 'CLOSE_FAILED'; | ||
} else if (dr == 5) { | ||
return 'ERROR'; | ||
} else if (dr == 6) { | ||
return 'CLOSED_REMOTELY'; | ||
} else if (dr == 7) { | ||
return 'CLOSED'; | ||
} else { | ||
return 'UNKNOWN['+dr+']'; | ||
} | ||
} | ||
|
||
var socket = new io.Socket(null, {}); | ||
socket.setMessageParser(socket.JSON_MESSAGE, { | ||
encode: function(obj) { | ||
return JSON.stringify(obj); | ||
}, | ||
decode: function(str) { | ||
return JSON.parse(str); | ||
} | ||
}); | ||
socket.connect(); | ||
socket.on('message', function(mtype, obj){ | ||
// if (mtype == 1) { | ||
message(obj); | ||
// } | ||
}); | ||
socket.on('connect', function(reason, error){ | ||
document.getElementById('form').style.display='block'; | ||
document.getElementById('chat').innerHTML = ''; | ||
}); | ||
socket.on('disconnect', function(reason, error){ | ||
addLine('<b>Disconnected['+drname(reason)+']: '+error+'</b>'); | ||
}); | ||
</script> | ||
|
||
<h1>Sample chat client</h1> | ||
<div id="chat"><p>Connecting...</p></div> | ||
<form id="form" onsubmit="send(); return false"> | ||
<input type="text" autocomplete="off" id="text"><input type="submit" value="Send"> | ||
</form> | ||
|
||
<style> | ||
#chat { height: 300px; overflow: auto; width: 800px; border: 1px solid #eee; font: 13px Helvetica, Arial; } | ||
#chat p { padding: 8px; margin: 0; } | ||
#chat p:nth-child(odd) { background: #F6F6F6; } | ||
#form { width: 782px; background: #333; padding: 5px 10px; display: none; } | ||
#form input[type=text] { width: 700px; padding: 5px; background: #fff; border: 1px solid #fff; } | ||
#form input[type=submit] { cursor: pointer; background: #999; border: none; padding: 6px 8px; -moz-border-radius: 8px; -webkit-border-radius: 8px; margin-left: 5px; text-shadow: 0 1px 0 #fff; } | ||
#form input[type=submit]:hover { background: #A2A2A2; } | ||
#form input[type=submit]:active { position: relative; top: 2px; } | ||
</style> | ||
|
||
</body> | ||
</html> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters