本文整理汇总了TypeScript中net.Socket.setEncoding方法的典型用法代码示例。如果您正苦于以下问题:TypeScript Socket.setEncoding方法的具体用法?TypeScript Socket.setEncoding怎么用?TypeScript Socket.setEncoding使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类net.Socket
的用法示例。
在下文中一共展示了Socket.setEncoding方法的5个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的TypeScript代码示例。
示例1: Buffer
messages.forEach((message: IMessage) => {
if (!config.readChat) {
return;
}
if ((!config.readMessage) && message.type === MESSAGE_TYPE_MESSAGE) {
return;
}
if ((!config.readBalloon) && message.type === MESSAGE_TYPE_BALLOON) {
return;
}
if ((!config.readAnnounce) && message.type === MESSAGE_TYPE_ANNOUNCE) {
return;
}
if ((!config.readLikes) && message.type === MESSAGE_TYPE_LIKES) {
return;
}
let client: Socket = new net.Socket();
client.setEncoding('binary');
client.on("error", () => {
mainWindow.webContents.send("error", "棒読みちゃんに接続できません。棒読みちゃんが起動していることを確認してください。");
});
client.connect(50001, "localhost", () => {
let text: string = message.message;
if (message.type === MESSAGE_TYPE_LIKES) {
text = "Eねされました。";
} else {
text = text.replace(/https?:\/\/[\-_\.!~*'\(\)a-zA-Z0-9;/\?:@&=\+\$,%#]+/g, "(URL省略)");
}
if (config.readNickname) {
text += " " + message.nickname;
}
let textBuffer: Buffer = new Buffer(text, 'utf8');
let headerBuffer: Buffer = new Buffer(15);
headerBuffer.writeUInt16LE(0x0001, 0); // command
headerBuffer.writeUInt16LE(0xFFFF, 2); // speed
headerBuffer.writeUInt16LE(0xFFFF, 4); // tone
headerBuffer.writeUInt16LE(0xFFFF, 6); // volume
headerBuffer.writeUInt16LE(0x0000, 8); // voice
headerBuffer.writeUInt8(0x00, 10); // charset
headerBuffer.writeUInt32LE(textBuffer.length, 11); // length
client.write(headerBuffer.toString('binary') + textBuffer.toString('binary'), 'binary', () => {
client.destroy();
});
});
});
示例2: createFd
private createFd(): void
{
if (this.m_fd != null)
return;
var self = this;
this.m_fd = new Socket();
/*
* We manage the encoding ourselves.
*
* Some servers/clients might be using ISO-646 or ISO-8859-1
*/
this.m_fd.setEncoding(null);
this.m_fd.on('data', data => self.onData(data));
this.m_fd.on('connect', d => self.onConnected());
this.m_fd.on('close', hadError => self.onClosed(hadError));
this.m_fd.on('error', error => self.onError(error));
}
示例3: speakWin
function speakWin(text: string): void {
let client: Socket = new net.Socket();
client.setEncoding('binary');
client.on("error", () => {
mainWindow.webContents.send("error", "棒読みちゃんに接続できません。棒読みちゃんが起動していることを確認してください。");
});
client.connect(50001, "localhost", () => {
let textBuffer: Buffer = new Buffer(text, 'utf8');
let headerBuffer: Buffer = new Buffer(15);
headerBuffer.writeUInt16LE(0x0001, 0); // command
headerBuffer.writeUInt16LE(0xFFFF, 2); // speed
headerBuffer.writeUInt16LE(0xFFFF, 4); // tone
headerBuffer.writeUInt16LE(0xFFFF, 6); // volume
headerBuffer.writeUInt16LE(0x0000, 8); // voice
headerBuffer.writeUInt8(0x00, 10); // charset
headerBuffer.writeUInt32LE(textBuffer.length, 11); // length
client.write(headerBuffer.toString('binary') + textBuffer.toString('binary'), 'binary', () => {
client.destroy();
});
});
}
示例4: constructor
constructor(
file: string,
args: ArgvOrCommandLine,
env: string[],
cwd: string,
cols: number,
rows: number,
debug: boolean,
private _useConpty: boolean | undefined
) {
if (this._useConpty === undefined || this._useConpty === true) {
this._useConpty = this._getWindowsBuildNumber() >= 18309;
}
if (this._useConpty) {
if (!conptyNative) {
conptyNative = loadNative('conpty');
}
} else {
if (!winptyNative) {
winptyNative = loadNative('pty');
}
}
this._ptyNative = this._useConpty ? conptyNative : winptyNative;
// Sanitize input variable.
cwd = path.resolve(cwd);
// Compose command line
const commandLine = argsToCommandLine(file, args);
// Open pty session.
let term: IConptyProcess | IWinptyProcess;
if (this._useConpty) {
term = (this._ptyNative as IConptyNative).startProcess(file, cols, rows, debug, this._generatePipeName());
} else {
term = (this._ptyNative as IWinptyNative).startProcess(file, commandLine, env, cwd, cols, rows, debug);
this._pid = (term as IWinptyProcess).pid;
this._innerPid = (term as IWinptyProcess).innerPid;
this._innerPidHandle = (term as IWinptyProcess).innerPidHandle;
}
// Not available on windows.
this._fd = term.fd;
// Generated incremental number that has no real purpose besides using it
// as a terminal id.
this._pty = term.pty;
// Create terminal pipe IPC channel and forward to a local unix socket.
this._outSocket = new Socket();
this._outSocket.setEncoding('utf8');
this._outSocket.connect(term.conout, () => {
// TODO: Emit event on agent instead of socket?
// Emit ready event.
this._outSocket.emit('ready_datapipe');
});
this._inSocket = new Socket();
this._inSocket.setEncoding('utf8');
this._inSocket.connect(term.conin);
// TODO: Wait for ready event?
if (this._useConpty) {
const connect = (this._ptyNative as IConptyNative).connect(this._pty, commandLine, cwd, env, this._$onProcessExit.bind(this));
this._innerPid = connect.pid;
}
}
示例5: Error
import * as net from 'net'
let argv: string[] = process.argv.slice(2)
const firstArg: string | undefined = argv.shift()
let port: string
if (firstArg === undefined) {
throw new Error('Expected port as the first argument')
} else {
port = firstArg
}
let client = new net.Socket()
client.setEncoding('utf8')
process.stdout.setEncoding('utf8')
process.stdin.setEncoding('utf8')
let isConnected = false
client.on('data', (data) => {
process.stdout.write(data.toString())
})
process.stdin.on('readable', () => {
let chunk = process.stdin.read()
if (chunk !== null) {
if (isConnected) {
client.write(chunk)
} else {
client.on('connect', () => {
client.write(chunk)
})
}