node. JS creates a WSS service

Source: Internet
Author: User

var https=require (' https '), var ws=require (' ws '), var fs=require (' FS '), Var keypath=process.cwd () + '/server.key ';// I put the secret key file in the directory under Run command to test var certpath=process.cwd () + '/server.crt ';//console.log (KeyPath);//console.log (Certpath); var options = {  Key:fs.readFileSync (keypath),  Cert:fs.readFileSync (Certpath),  passphrase: ' 1234 '// If the secret key file has a password, use this property to set the password}; var server=https.createserver (options, function (req, res) {//If the simple HTTPS connection will return this thing    Res.writehead (403);//403    Res.end ("This is a  WebSockets server!\n");}). Listen (25550);  var wss = new ws. Server ({server:server}),//To throw the created HTTPS server into the WebSocket creation function, WS will use this server to create the WSS service//. If an HTTP service is thrown in, then the WS-Service Wss.on (' Connection ', function (wsconnect) {    wsconnect.on (' message '), function ( Message) {        console.log (message);    });});

  

node. JS creates a WSS service

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.