Created
November 20, 2011 14:20
-
-
Save zed/1380305 to your computer and use it in GitHub Desktop.
WebSocket Echo :python:twisted:txws:jquery:
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
/twistd.pid | |
/_trial_temp/ |
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
<!-- http://www.tavendo.de/autobahn/tutorial/echo.html --> | |
<html> | |
<head> | |
<script type="text/javascript"> | |
window.onload = function() { | |
var ws_uri = "ws://localhost:8076"; | |
if ("WebSocket" in window) { | |
webSocket = new WebSocket(ws_uri); | |
} | |
else { | |
// Firefox 7/8 currently prefixes the WebSocket object | |
webSocket = new MozWebSocket(ws_uri); | |
} | |
webSocket.onmessage = function(e) { | |
console.log("Got echo: " + e.data); | |
} | |
} | |
</script> | |
</head> | |
<body> | |
<h1>Autobahn WebSockets Echo Test</h1> | |
<button onclick='webSocket.send("Hello, world!");'>Send Hello</button> | |
</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
<!doctype html> | |
<title>Send keys using websocket and echo the response</title> | |
<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.7.0/jquery.min.js"> | |
</script> | |
<script src="sendkeys.js"></script> | |
<input type=text id=entry value="type something"> | |
<div id=output>Here you should see the typed text in UPPER case</div> | |
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
// send keys to websocket and echo the response | |
$(document).ready(function() { | |
// create websocket | |
if (! ("WebSocket" in window)) WebSocket = MozWebSocket; // firefox | |
var socket = new WebSocket("ws://localhost:8076"); | |
// open the socket | |
socket.onopen = function(event) { | |
socket.send('connected'); | |
// show server response | |
socket.onmessage = function(e) { | |
$("#output").text(e.data); | |
} | |
// for each typed key send #entry's text to server | |
$("#entry").keyup(function (e) { | |
socket.send($("#entry").attr("value")); | |
}); | |
} | |
}); |
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
# -*- test-case-name: test_streamer -*- | |
"""WebSocket Echo. | |
Install: pip install twisted txws | |
Run: twistd -ny streamer.py | |
Visit http://localhost:8080/ | |
""" | |
from twisted.application import strports # pip install twisted | |
from twisted.application.service import Application | |
from twisted.internet.protocol import Factory, Protocol | |
from twisted.python import log | |
from txws import WebSocketFactory # pip install txws | |
class EchoUpper(Protocol): | |
"""Echo uppercased.""" | |
def dataReceived(self, data): | |
log.msg("Got %r" % (data,)) | |
self.transport.write(data.upper()) | |
application = Application("ws-streamer") | |
echofactory = Factory() | |
echofactory.protocol = EchoUpper # use Factory.buildProtocol() | |
service = strports.service("tcp:8076:interface=127.0.0.1", | |
WebSocketFactory(echofactory)) | |
service.setServiceParent(application) | |
from twisted.web.server import Site | |
from twisted.web.static import File | |
resource = File('.') # serve current directory | |
webservice = strports.service("tcp:8080:interface=127.0.0.1", | |
Site(resource)) | |
webservice.setServiceParent(application) | |
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
from twisted.test import proto_helpers | |
from twisted.trial import unittest | |
from streamer import EchoUpper | |
class EchoUpperTestCase(unittest.TestCase): | |
def setUp(self): | |
self.proto = EchoUpper() | |
self.tr = proto_helpers.StringTransport() | |
self.proto.makeConnection(self.tr) | |
def test_dataReceived(self): | |
self.proto.dataReceived('abc') | |
self.assertEqual(self.tr.value(), 'ABC') |
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
<!DOCTYPE html> | |
<!-- http://websocket.org/echo.html --> | |
<meta charset="utf-8" /> | |
<title>WebSocket Test</title> | |
<script language="javascript" type="text/javascript"> | |
var wsUri = "ws://localhost:8076/"; | |
var output; | |
function init() | |
{ | |
output = document.getElementById("output"); | |
testWebSocket(); | |
} | |
function testWebSocket() | |
{ | |
if (! ("WebSocket" in window)) WebSocket = MozWebSocket; // firefox | |
websocket = new WebSocket(wsUri); | |
websocket.onopen = function(evt) { onOpen(evt) }; | |
websocket.onclose = function(evt) { onClose(evt) }; | |
websocket.onmessage = function(evt) { onMessage(evt) }; | |
websocket.onerror = function(evt) { onError(evt) }; | |
} | |
function onOpen(evt) | |
{ | |
writeToScreen("CONNECTED"); | |
doSend("WebSocket rocks"); | |
} | |
function onClose(evt) | |
{ | |
writeToScreen("DISCONNECTED"); | |
} | |
function onMessage(evt) | |
{ | |
writeToScreen('<span style="color: blue;">RESPONSE: ' + evt.data+'</span>'); | |
websocket.close(); | |
} | |
function onError(evt) | |
{ | |
writeToScreen('<span style="color: red;">ERROR:</span> ' + evt.data); | |
} | |
function doSend(message) | |
{ | |
writeToScreen("SENT: " + message); | |
websocket.send(message); | |
} | |
function writeToScreen(message) | |
{ | |
var pre = document.createElement("p"); | |
pre.style.wordWrap = "break-word"; | |
pre.innerHTML = message; | |
output.appendChild(pre); | |
} | |
window.addEventListener("load", init, false); | |
</script> | |
<h2>WebSocket Test</h2> | |
<div id="output"></div> | |
</html> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment