Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <!DOCTYPE html>
- <html>
- <head>
- <title>WebSockets Client</title>
- <script src="http://code.jquery.com/jquery-1.9.1.min.js"></script>
- </head>
- <body>
- Enter text to send to the websocket server:
- <div id="send">
- <input type="text" id="data" size="100"/><br>
- <input type="button" id="sendtext" value="send text"/>
- </div>
- <div id="output"></div>
- </body>
- </html>
- <script>
- jQuery(function($){
- if (!("WebSocket" in window)) {
- alert("Your browser does not support web sockets");
- }else{
- setup();
- }
- function setup(){
- // Note: You have to change the host var
- // if your client runs on a different machine than the websocket server
- var host = "ws://localhost:9090/ws";
- var socket = new WebSocket(host);
- //console.log("socket status: " + socket.readyState);
- var $txt = $("#data");
- var $btnSend = $("#sendtext");
- $txt.focus();
- // event handlers for UI
- $btnSend.on('click',function(){
- var text = $txt.val();
- if(text == ""){
- return;
- }
- socket.send(text);
- $txt.val("");
- });
- $txt.keypress(function(evt){
- if(evt.which == 13){
- $btnSend.click();
- }
- });
- // event handlers for websocket
- if(socket){
- socket.onopen = function(){
- //alert("connection opened....");
- }
- socket.onmessage = function(msg){
- showServerResponse(msg.data);
- }
- socket.onclose = function(){
- //alert("connection closed....");
- showServerResponse("The connection has been closed.");
- }
- }else{
- console.log("invalid socket");
- }
- function showServerResponse(txt){
- var p = document.createElement('p');
- p.innerHTML = txt;
- document.getElementById('output').appendChild(p);
- }
- }
- });
- </script>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement