Search
 
SCRIPT & CODE EXAMPLE
 

CSHARP

web socket background.js example

function createWebSocketConnection() {
    if('WebSocket' in window){
        chrome.storage.local.get("instance", function(data) {
            connect('wss://' + data.instance + '/ws/demoPushNotifications');
        });
    }
}

//Make a websocket connection with the server.
function connect(host) {
    if (websocket === undefined) {
        websocket = new WebSocket(host);
    }

    websocket.onopen = function() {
        chrome.storage.local.get(["username"], function(data) {
            websocket.send(JSON.stringify({userLoginId: data.username}));
        });
    };

    websocket.onmessage = function (event) {
        var received_msg = JSON.parse(event.data);
        var demoNotificationOptions = {
            type: "basic",
            title: received_msg.subject,
            message: received_msg.message,
            iconUrl: "images/demo-icon.png"
        }
        chrome.notifications.create("", demoNotificationOptions);
        updateToolbarBadge();
    };

    //If the websocket is closed but the session is still active, create new connection again
    websocket.onclose = function() {
        websocket = undefined;
        chrome.storage.local.get(['demo_session'], function(data) {
            if (data.demo_session) {
                createWebSocketConnection();
            }
        });
    };
}

//Close the websocket connection
function closeWebSocketConnection(username) {
    if (websocket != null || websocket != undefined) {
        websocket.close();
        websocket = undefined;
    }
}
Comment

PREVIOUS NEXT
Code Example
Csharp :: Implementing Banner Ads Unity 
Csharp :: closing main window after clicking on a button that opens another window in wpf 
Csharp :: satisfactory controller support 
Csharp :: return last row if all other condition fails in linq c# 
Csharp :: ms transform 
Csharp :: c# lambda get all records async 
Csharp :: c# webclient accept all certificates 
Csharp :: c# aspx return image 
Csharp :: c# check if list is empty 
Csharp :: c# control datagridview null value 
Csharp :: c# download to string 
Csharp :: Maximum Points You Can Obtain from Cards 
Csharp :: unity organize variables in inspector 
Csharp :: c# get executing method name 
Csharp :: blender how to switch cameras 
Csharp :: C# Payroll 
Csharp :: blazor OnInitializedAsync Unhandled exception rendering component: Cannot wait on monitors on this runtime. 
Csharp :: compass direction mobile unity 
Csharp :: params keycord as var name c# 
Csharp :: unity wheelcollider antiroll 
Csharp :: downloading a large file asp boilerplate (abp) 
Csharp :: how to create advance search with parameter in asp.net mvc 
Csharp :: unity custom editor hide values in dropdown list 
Csharp :: unity only one component type 
Csharp :: unity Texture2D efficient performance draw pixels 
Csharp :: C# .net JwtSecurityTokenHandler jwttoken claims to object 
Csharp :: new bitmap pixel format c# 
Csharp :: palindromes 
Csharp :: unity pause 
Csharp :: how to get day name from datetimepicker in c# 
ADD CONTENT
Topic
Content
Source link
Name
5+5 =