Web Socket Server (using node.js)

Slides:



Advertisements
Similar presentations
COEN 445 Communication Networks and Protocols Lab 4
Advertisements

A CHAT CLIENT-SERVER MODULE IN JAVA BY MAHTAB M HUSSAIN MAYANK MOHAN ISE 582 FALL 2003 PROJECT.
HTTP Overview Vijayan Sugumaran School of Business Administration Oakland University.
Process-to-Process Delivery:
WebSockets [intro].
Ajax (Asynchronous JavaScript and XML). AJAX  Enable asynchronous communication between a web client and a server.  A client is not blocked when an.
Assignment 3 A Client/Server Application: Chatroom.
Simple Web Services. Internet Basics The Internet is based on a communication protocol named TCP (Transmission Control Protocol) TCP allows programs running.
Comp2513 Forms and CGI Server Applications Daniel L. Silver, Ph.D.
Simple Web Services. Internet Basics The Internet is based on a communication protocol named TCP (Transmission Control Protocol) TCP allows programs running.
Silberschatz, Galvin and Gagne  2002 Modified for CSCI 399, Royden, Operating System Concepts Operating Systems Lecture 12 Communicating over.
FTP Client Application CSC 8560 Brian Jorgage 4/27/2004.
Internet Applications and Network Programming Dr. Abraham Professor UTPA.
Chapter 2 Applications and Layered Architectures Sockets.
1 Chapter 28 Networking. 2 Objectives F To comprehend socket-based communication in Java (§28.2). F To understand client/server computing (§28.2). F To.
Form Data Encoding GET – URL encoded POST – URL encoded
Zz SOCKET.IO going real time with Arnout Kazemier
Copyright © 2008 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Chapter 1 Fundamentals.
CSI 3125, Preliminaries, page 1 Networking. CSI 3125, Preliminaries, page 2 Networking A network represents interconnection of computers that is capable.
Network Architecture Protocol hierarchies Design Issues for the layers
Simple Web Services. Internet Basics The Internet is based on a communication protocol named TCP (Transmission Control Protocol) TCP allows programs running.
USING ANDROID WITH THE INTERNET. Slide 2 Lecture Summary Getting network permissions Working with the HTTP protocol Sending HTTP requests Getting results.
Liang, Introduction to Java Programming, Ninth Edition, (c) 2013 Pearson Education, Inc. All rights reserved. 1 Chapter 33 Networking.
Tiny http client and server
Assignment 3 A Client/Server Application: Chatroom
Not a Language but a series of techniques
Content from Python Docs.
Computing with C# and the .NET Framework
XMLHttp Object.
Networks and Client/Server Applications
Internet Networking recitation #12
Sockets and URLs 17-Sep-18.
IS333D: MULTI-TIER APPLICATION DEVELOPMENT
WebSocket: Full-Duplex Solution for the Web
Uniform Resource Locators
2017, Fall Pusan National University Ki-Joune Li
WEB API.
Sockets and URLs 13-Nov-18.
CSE 154 Lecture 22: AJAX.
Clients and Servers 19-Nov-18.
Application layer Lecture 7.
Chapter 9 Web Services: JAX-RPC, WSDL, XML Schema, and SOAP
Process-to-Process Delivery:
WebSocket 101 Shuai Zhao.
Clients and Servers 1-Dec-18.
Sockets and URLs 3-Dec-18.
Uniform Resource Locators (URLs)
Starting TCP Connection – A High Level View
Introduction to HTML5 and WebSockets.
Web Programming Language
Web Socket Protocol.
Internet Applications and Network Programming
JavaScript & jQuery AJAX.
HyperText Transfer Protocol
TA: Donghyun (David) Kim
Lecture 5: Functions and Parameters
Uniform Resource Locators
Lecture 14: JSON and Web SERVICES
Chengyu Sun California State University, Los Angeles
Web APIs API = Application Programmer Interface
Chengyu Sun California State University, Los Angeles
Process-to-Process Delivery: UDP, TCP
Uniform Resource Locators (URLs)
Message Passing Systems Version 2
Chengyu Sun California State University, Los Angeles
Clients and Servers 19-Jul-19.
Clients and Servers 13-Sep-19.
Exceptions and networking
Message Passing Systems
Presentation transcript:

Web Socket Server (using node.js)

Web Socket Server Often, an HTTP server is used to detect WebSocket handshakes. WebSocket server can be built using many programming languages: Javascript: Socket.io Node.js Ruby: EventMachine Faye Python: pyWebSocket Tornado C++: uWebSockets C#: Fleck Java: Jetty .NET: SuperWebSocket Go: Gorilla

Web Socket Server Required Steps to Create a Web Socket Server in Node.JS Create instance of an http server and listen to a specific port Create a web socket server Listen for connections Callback for connections

Create HTTP server and listen var http = require('http'); var server = http.createServer(function(request, response) { }); server.listen(1234, function() { console.log((new Date()) + ' Server is listening on port 1234');

Create Web Socket Server var WebSocketServer = require('websocket').server; wsServer = new WebSocketServer({ httpServer: server }); wsServer.on('request', function(r){ // Code here to run on connection

Web Socket Server Methods mount(serverConfig): Attach the WebSocketServer instance to a Node http.Server instance. unmount(): Detach the WebSocketServer instance from the Node http.Server. All existing connections are left alone and will not be affected, but no new WebSocket connections will be accepted. closeAllConnections():close all open WebSocket connections. shutDown():closes all open WebSocket connections and unmounts the server from the Node http.Server instance.

Web Socket Server Events request, function(webSocketRequest) If autoAcceptConnections is set to false, a request event will be emitted by the server whenever a new WebSocket request is made. connect, function(webSocketConnection) Emitted whenever a new WebSocket connection is accepted. close, function(webSocketConnection, closeReason, description) Whenever a connection is closed for any reason, the WebSocketServer instance will emit a close event, passing a reference to the WebSocketConnection instance that was closed.

WebSocketRequest Properties host: A string containing the contents of the Host header passed by the client. This will include the port number if a non-standard port is used. www.example.com www.example.com:8080 127.0.0.1:3000 resource : A string containing the path that was requested by the client. resourceURL : A Node URL object containing the parsed resource, including the query string parameters. remoteAddress: The remote client's IP Address as a string. requestedProtocols : An array containing a list of strings that indicate the subprotocols the client would like to speak.

WebSocket Request Methods accept(acceptedProtocol, allowedOrigin) Returns: WebSocketConnection instance call this function on the request object to accept the connection. If you don't have a particular subprotocol you wish to speak, you may pass null for the acceptedProtocol parameter. acceptedProtocol parameter is case-insensitive, and you must either pass a value that was originally requested by the client or null. The return value can be used to communicate with the connected client. reject([httpStatus], [reason]) If you decide to reject the connection, you must call reject. You may optionally pass an HTTP Status code (such as 404) and a textual description.

WebSocketRequest Events requestAccepted , function(webSocketConnection) Emitted by the WebSocketRequest object when the accept method has been called and the connection has been established. webSocketConnection is the established WebSocketConnection instance that can be used to communicate with the connected client. requestRejected, function() Emitted by the WebSocketRequest object when the reject method has been called and the connection has been terminated.

Accepting Connection Accepting a connection Store connected clients Listen for incoming messages Listen for a client disconnecting var count = 0; var clients = {}; var connection = r.accept(null, r.origin);

Accepting Connection Accepting a connection Store connected clients Listen for incoming messages Listen for a client disconnecting var count = 0; var clients = {}; var connection = r.accept('echo-protocol', r.origin); clients[count++] = connection console.log((new Date()) + ' Connection accepted');

WebSocket Connection Properties closeDescription: After the connection is closed, contains a textual description of the reason. closeReasonCode: After the connection is closed, contains the numeric close reason status code protocol: The subprotocol that was chosen to be spoken on this connection. This field will have been converted to lower case. remoteAddress: The IP address of the remote peer as a string. connected: A boolean value indicating whether or not the connection is still connected.

WebSocket Connection Methods close([reasonCode], [description]): close the connection. drop([reasonCode], [description]): immediately close the socket without waiting for a response. sendUTF(string): Immediately sends the specified string as a UTF-8 WebSocket message to the remote peer. sendBytes(buffer): Immediately sends the specified Buffer object as a Binary WebSocket message to the remote peer. send(data): auto-detect the data type and send the appropriate message. If data is a Node Buffer, a binary message will be sent. Otherwise, the object provided must implement the toString() method.

WebSocket Connection Events message , function(message) Emitted whenever a complete message is received. utf8Data binaryData close, function(reasonCode, description) This event is emitted when the connection has been fully closed. error, function(error) This event is emitted when there has been a socket error.

Listen for incoming messages connection.on('message', function(message) { var msgString = message.utf8Data; for(var i in clients){ clients[i].sendUTF(msgString); } });

Listen for client disconnecting connection.on('close', function(reasonCode, description) { delete clients[id]; console.log((new Date()) + connection.remoteAddress + ' isconnected.'); });

More Info https://github.com/theturtle32/WebSocket-Node