Created
December 2, 2011 03:49
-
-
Save aslakhellesoy/1421652 to your computer and use it in GitHub Desktop.
Webbit Chat Server
This file contains hidden or 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
| body { | |
| font-family: arial, sans-serif; | |
| padding: 10px; | |
| } | |
| #chatlog, #entry { | |
| width: 100%; | |
| border: solid 4px #000000; | |
| font-weight: bold; | |
| font-family: monospace; | |
| font-size: 30px; | |
| } |
This file contains hidden or 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
| window.onload = function() { | |
| var ws = new WebSocket('ws://' + document.location.host + '/chat'); | |
| ws.onmessage = function(e) { | |
| var chatlog = document.getElementById('chatlog'); | |
| chatlog.value += e.data + '\n'; | |
| }; | |
| function send(text) { | |
| ws.send(text); | |
| } | |
| var entry = document.getElementById('entry'); | |
| entry.onkeypress = function(e) { | |
| if(entry.value && e.keyCode == 13) { | |
| send(entry.value); | |
| entry.value = ''; | |
| } | |
| }; | |
| }; |
This file contains hidden or 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
| <!DOCTYPE html> | |
| <html> | |
| <head> | |
| <title>Chat room</title> | |
| <link rel="stylesheet" type="text/css" href="chatroom.css"> | |
| <script type="text/javascript" src="chatroom.js"></script> | |
| </head> | |
| <body> | |
| <textarea id="chatlog" rows="5"></textarea> | |
| <input type="text" id="entry"> | |
| </body> | |
| </html> |
This file contains hidden or 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
| package yow.webbit.chat; | |
| import org.webbitserver.*; | |
| import org.webbitserver.handler.StaticFileHandler; | |
| import org.webbitserver.handler.logging.LoggingHandler; | |
| import org.webbitserver.handler.logging.SimpleLogSink; | |
| import org.webbitserver.netty.NettyWebServer; | |
| import java.io.IOException; | |
| import java.util.HashSet; | |
| import java.util.Set; | |
| public class Main { | |
| public static void main(String[] args) throws IOException { | |
| WebServer server = new NettyWebServer(8080); | |
| server.add(new LoggingHandler(new SimpleLogSink())); | |
| server.add(new StaticFileHandler("src/main/resources/web")); | |
| server.add("/chat", new WebSocketHandler() { | |
| public Set<WebSocketConnection> connections = new HashSet<WebSocketConnection>(); | |
| public void onOpen(WebSocketConnection connection) throws Exception { | |
| connections.add(connection); | |
| } | |
| public void onClose(WebSocketConnection connection) throws Exception { | |
| connections.remove(connection); | |
| } | |
| public void onMessage(WebSocketConnection connection, String msg) throws Throwable { | |
| for (WebSocketConnection ws : connections) { | |
| ws.send(msg); | |
| } | |
| } | |
| public void onMessage(WebSocketConnection connection, byte[] msg) throws Throwable { | |
| throw new UnsupportedOperationException(); | |
| } | |
| public void onPong(WebSocketConnection connection, String msg) throws Throwable { | |
| throw new UnsupportedOperationException(); | |
| } | |
| }); | |
| server.start(); | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment