Websocket

Develop

  1. Create a new project and name it websocket_basic.
  2. Select the ScriptingServices sub-folder of the project and open the pop-up menu.
  3. Choose New -> Scripting Service.
  4. Choose Server-Side JavaScript Service from the list of available templates.
  5. Give it a meaningful name (e.g websocket_basic.js).
  6. Replace the generated code in websocket_basic .js with the following:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
	/* globals $ */
	/* eslint-env node, dirigible */

	var globals = require("core/globals");
	var websocket = require("net/websocket");
	var context = require("core/context");

	var websocketSession = websocket.getSession();
	var type = context.get("type");
	var from = context.get("from");
	var to = context.get("to");
	var message = context.get("message");

	if (type == "welcome") {
		websocketSession.sendText("Welcome " + from + "!");
		globals.set("chat-session-" + from, websocketSession);
		var chatMembers = globals.get("chat-members");
		if (chatMembers === null) {
			chatMembers = [];
			globals.set("chat-members", chatMembers);
		}
		chatMembers.push(from);
		console.error("registered " + from);
	}

	if (type == "message") {
		var toSession = globals.get("chat-session-" + to);
		if (toSession !== null) {
			toSession.sendText(message);
			console.error("send to " + to);
		} else {
			console.error("no session for " + to);
		}
	}

	if (type == "list") {
		var chatMembers = globals.get("chat-members");
		console.error("list: " + JSON.stringify(chatMembers));
		websocketSession.sendText(JSON.stringify(chatMembers));
	}


	/**
	-- Client 1
	var jsSocket = new WebSocket("ws://localhost:8080/service/js");
	jsSocket.send('{"module": "/chat/chat_service.js","params": {"type":"welcome", "from":"Tom"}}');
	-- Client 2
	var jsSocket = new WebSocket("ws://localhost:8080/service/js");
	jsSocket.send('{"module": "/chat/chat_service.js","params": {"type":"welcome", "from":"Jerry"}}');
	-- Client 1
	jsSocket.send('{"module": "/chat/chat_service.js","params": {"type":"message", "from":"Tom", "to":"Jerry", "message": "Hello Jerry!"}}');
	-- Client 2
	jsSocket.send('{"module": "/chat/chat_service.js","params": {"type":"message", "from":"Jerry", "to":"Tom", "message": "Hi Tom!"}}');
	jsSocket.send('{"module": "/chat/chat_service.js","params": {"type":"list", "from":"Tom"}}');
	*/
	

Discover

To discover all available services, you can go to the Registry.

  1. From the main menu, choose Window -> Show Perspective -> Registry.
  2. The Registry perspective represents a view to the enabled runtime content. From its menu, choose Discover -> JavaScript to open the currently available server-side JavaScript service endpoints.
  3. You can see the list of available endpoints, where you can find yours by naming convention: {project}/{service path}
Edit