本文整理匯總了TypeScript中zmq.socket函數的典型用法代碼示例。如果您正苦於以下問題:TypeScript socket函數的具體用法?TypeScript socket怎麽用?TypeScript socket使用的例子?那麽, 這裏精選的函數代碼示例或許可以為您提供幫助。
在下文中一共展示了socket函數的6個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的TypeScript代碼示例。
示例1: setInterval
var playGame = (gameData) => {
var id:string = gameData.name;
console.log(id + " is starting to reek havoc");
var controlSocket = zmq.socket('push');
controlSocket.connect('tcp://' + server + ':' + control_port);
// These chaos monkeys don't even need to watch the state socket
var step = () => {
var f = 1,
r = 0;
controlSocket.send(gameData.secret + "," + f + "," + r);
if (gameNotOver) {
setInterval(step, UPDATE_RATE);
}
};
step()
};
示例2: connect
export function connect() {
var requester = zmq.socket('req');
var x = 0;
requester.on("message", (reply) => {
console.log("Received reply", x, ": [", reply.toString(), ']');
if (x === 10) {
requester.close();
}
})
requester.connect("tcp://localhost:5555");
for (var i = 0; i < 10; i++) {
console.log("Sending request", i, 'âŚ');
requester.send("Hello");
}
process.on('SIGINT', function() {
requester.close();
});
}
示例3: function
// Websocket component
io.on('connection', function(ws){
console.log('A websocket connected');
ws.on('disconnect', function(){
console.log('Websocket disconnected');
});
});
// ZMQ component
console.log('Frontend is trying to connect to ' + server);
var lobbySocket = zmq.socket('req');
var logSocket = zmq.socket('sub');
var gameStateSocket = zmq.socket('sub');
//lobbySocket.subscribe("");
logSocket.subscribe("");
gameStateSocket.subscribe("");
// On receiving a message from the game server
// Note the `msg` is a string or buffer, not JSON
lobbySocket.on('message', function(msg){
console.log('[LOBBY] Received: %s', msg.toString());
});
示例4:
/// <reference path="../../typings/tsd.d.ts" />
"use strict";
import * as fs from 'fs';
import * as zmq from 'zmq';
// Create publisher endpoint
let publisher = zmq.socket('pub');
let filename = process.argv[2];
fs.watch(filename, () => {
// send the message to any subscribers
publisher.send(JSON.stringify({
type: 'changed',
file: filename,
timestamp: Date.now()
}));
});
publisher.bind('tcp://*:5444', (err) => {
if(err) { throw err; }
console.log('Listening for ZMQ subscribers');
});
示例5: require
var zmq = require('zmq');
var UPDATE_RATE = 30; // [ms]
var NUM_SHIPS = parseInt(process.argv.length > 2 ? process.argv[2] : '10');
var server = process.argv.length > 3 ? process.argv[3] : 'localhost';
var state_port = 5556;
var control_port = 5557;
var lobby_port = 5558;
// Share one state socket
var latestState;
var gameNotOver = true;
var stateSocket = zmq.socket('sub');
var lobbySocket = zmq.socket('req');
console.log("Connecting to lobby");
lobbySocket.connect('tcp://' + server + ':' + lobby_port);
lobbySocket.on('message', (msg) => {
var data = JSON.parse(msg);
console.log("LOBBY: ", data);
playGame(data);
});
stateSocket.subscribe('');
stateSocket.on('message', function(topic, msg) {
console.log('[GAME] Received State for: %s', topic.toString());
latestState = JSON.parse(msg.toString());
示例6: Date
/// <reference path="../../typings/tsd.d.ts" />
"use strict";
import * as zmq from 'zmq';
interface Message {
type: string
file: string
timestamp: number
}
let subscriber = zmq.socket('sub');
subscriber.subscribe(''); // subscribe to all the messages
subscriber.on('message', (data) => {
let message: Message = JSON.parse(data.toString());
let {type, file, timestamp} = message;
let date = new Date(timestamp);
console.log(`File ${file} changed at ${date}`);
})
subscriber.connect('tcp://localhost:5444');