Skip to content

Commit

Permalink
Fix web socket examples
Browse files Browse the repository at this point in the history
  • Loading branch information
h3poteto committed Mar 11, 2024
1 parent e07c171 commit a91a460
Show file tree
Hide file tree
Showing 5 changed files with 134 additions and 132 deletions.
52 changes: 26 additions & 26 deletions example/typescript/src/firefish/web_socket.ts
Original file line number Diff line number Diff line change
@@ -1,39 +1,39 @@
import generator, { Entity, WebSocketInterface } from 'megalodon'
import generator, { Entity } from 'megalodon'
import log4js from 'log4js'

const BASE_URL: string = process.env.FIREFISH_STREAMING_URL!
const BASE_URL: string = process.env.FIREFISH_URL!
const access_token: string = process.env.FIREFISH_ACCESS_TOKEN!

const client = generator('firefish', BASE_URL, access_token)

const stream: WebSocketInterface = client.userSocket()
client.localStreaming().then(stream => {
const logger = log4js.getLogger()
logger.level = 'debug'
stream.on('connect', () => {
logger.debug('connect')
})

const logger = log4js.getLogger()
logger.level = 'debug'
stream.on('connect', () => {
logger.debug('connect')
})
stream.on('pong', () => {
logger.debug('pong')
})

stream.on('pong', () => {
logger.debug('pong')
})
stream.on('update', (status: Entity.Status) => {
logger.debug(status)
})

stream.on('update', (status: Entity.Status) => {
logger.debug(status)
})
stream.on('notification', (notification: Entity.Notification) => {
logger.debug(notification)
})

stream.on('notification', (notification: Entity.Notification) => {
logger.debug(notification)
})
stream.on('error', (err: Error) => {
console.error(err)
})

stream.on('error', (err: Error) => {
console.error(err)
})

stream.on('close', () => {
logger.debug('close')
})
stream.on('close', () => {
logger.debug('close')
})

stream.on('parser-error', (err: Error) => {
console.error(err)
stream.on('parser-error', (err: Error) => {
console.error(err)
})
})
65 changes: 33 additions & 32 deletions example/typescript/src/mastodon/streaming.ts
Original file line number Diff line number Diff line change
@@ -1,48 +1,49 @@
import generator, { Entity, WebSocketInterface } from 'megalodon'
import generator, { Entity } from 'megalodon'

const BASE_URL: string = process.env.MASTODON_STREAMING_URL!
const BASE_URL: string = process.env.MASTODON_URL!

const access_token: string = process.env.MASTODON_ACCESS_TOKEN!

const client = generator('mastodon', BASE_URL, access_token)

const stream: WebSocketInterface = client.localSocket()
stream.on('connect', () => {
console.log('connect')
})
client.localStreaming().then(stream => {
stream.on('connect', () => {
console.log('connect')
})

stream.on('pong', () => {
console.log('pong')
})
stream.on('pong', () => {
console.log('pong')
})

stream.on('update', (status: Entity.Status) => {
console.log(status)
})
stream.on('update', (status: Entity.Status) => {
console.log(status)
})

stream.on('notification', (notification: Entity.Notification) => {
console.log(notification)
})
stream.on('notification', (notification: Entity.Notification) => {
console.log(notification)
})

stream.on('delete', (id: number) => {
console.log(id)
})
stream.on('delete', (id: number) => {
console.log(id)
})

stream.on('error', (err: Error) => {
console.error(err)
})
stream.on('error', (err: Error) => {
console.error(err)
})

stream.on('status_update', (status: Entity.Status) => {
console.log('updated: ', status.url)
})
stream.on('status_update', (status: Entity.Status) => {
console.log('updated: ', status.url)
})

stream.on('heartbeat', () => {
console.log('thump.')
})
stream.on('heartbeat', () => {
console.log('thump.')
})

stream.on('close', () => {
console.log('close')
})
stream.on('close', () => {
console.log('close')
})

stream.on('parser-error', (err: Error) => {
console.error(err)
stream.on('parser-error', (err: Error) => {
console.error(err)
})
})
67 changes: 34 additions & 33 deletions example/typescript/src/mastodon/unauthorized_streaming.ts
Original file line number Diff line number Diff line change
@@ -1,52 +1,53 @@
import generator, { Entity, WebSocketInterface } from 'megalodon'
import generator, { Entity } from 'megalodon'

declare var process: {
env: {
MASTODON_STREAMING_URL: string
MASTODON_URL: string
}
}

const url: string = process.env.MASTODON_STREAMING_URL
const url: string = process.env.MASTODON_URL

const client = generator('mastodon', url)

const stream: WebSocketInterface = client.localSocket()
stream.on('connect', () => {
console.log('connect')
})
client.localStreaming().then(stream => {
stream.on('connect', () => {
console.log('connect')
})

stream.on('pong', () => {
console.log('pong')
})
stream.on('pong', () => {
console.log('pong')
})

stream.on('update', (status: Entity.Status) => {
console.log(status)
})
stream.on('update', (status: Entity.Status) => {
console.log(status)
})

stream.on('notification', (notification: Entity.Notification) => {
console.log(notification)
})
stream.on('notification', (notification: Entity.Notification) => {
console.log(notification)
})

stream.on('delete', (id: number) => {
console.log(id)
})
stream.on('delete', (id: number) => {
console.log(id)
})

stream.on('error', (err: Error) => {
console.error(err)
})
stream.on('error', (err: Error) => {
console.error(err)
})

stream.on('status_update', (status: Entity.Status) => {
console.log('updated: ', status.url)
})
stream.on('status_update', (status: Entity.Status) => {
console.log('updated: ', status.url)
})

stream.on('heartbeat', () => {
console.log('thump.')
})
stream.on('heartbeat', () => {
console.log('thump.')
})

stream.on('close', () => {
console.log('close')
})
stream.on('close', () => {
console.log('close')
})

stream.on('parser-error', (err: Error) => {
console.error(err)
stream.on('parser-error', (err: Error) => {
console.error(err)
})
})
70 changes: 35 additions & 35 deletions example/typescript/src/pleroma/web_socket.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import generator, { Entity, WebSocketInterface } from 'megalodon'
import generator, { Entity } from 'megalodon'
import log4js from 'log4js'

declare var process: {
Expand All @@ -7,52 +7,52 @@ declare var process: {
}
}

const BASE_URL: string = 'wss://pleroma.io'
const BASE_URL: string = 'https://pleroma.io'

const access_token: string = process.env.PLEROMA_ACCESS_TOKEN

const client = generator('pleroma', BASE_URL, access_token)

const stream: WebSocketInterface = client.userSocket()
client.userStreaming().then(stream => {
const logger = log4js.getLogger()
logger.level = 'debug'
stream.on('connect', () => {
logger.debug('connect')
})

const logger = log4js.getLogger()
logger.level = 'debug'
stream.on('connect', () => {
logger.debug('connect')
})
stream.on('pong', () => {
logger.debug('pong')
})

stream.on('pong', () => {
logger.debug('pong')
})
stream.on('update', (status: Entity.Status) => {
logger.debug(status.url)
})

stream.on('update', (status: Entity.Status) => {
logger.debug(status.url)
})

stream.on('notification', (notification: Entity.Notification) => {
logger.debug(notification)
})
stream.on('notification', (notification: Entity.Notification) => {
logger.debug(notification)
})

stream.on('delete', (id: number) => {
logger.debug(id)
})
stream.on('delete', (id: number) => {
logger.debug(id)
})

stream.on('error', (err: Error) => {
console.error(err)
})
stream.on('error', (err: Error) => {
console.error(err)
})

stream.on('heartbeat', () => {
logger.debug('thump.')
})
stream.on('heartbeat', () => {
logger.debug('thump.')
})

stream.on('status_update', (status: Entity.Status) => {
logger.debug('updated: ', status.url)
})
stream.on('status_update', (status: Entity.Status) => {
logger.debug('updated: ', status.url)
})

stream.on('close', () => {
logger.debug('close')
})
stream.on('close', () => {
logger.debug('close')
})

stream.on('parser-error', (err: Error) => {
logger.error(err)
stream.on('parser-error', (err: Error) => {
logger.error(err)
})
})
12 changes: 6 additions & 6 deletions megalodon/src/pleroma.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3201,32 +3201,32 @@ export default class Pleroma implements MegalodonInterface {
}

public async userStreaming(): Promise<WebSocket> {
const url = this.streamingURL()
const url = await this.streamingURL()
return this.client.socket(`${url}/api/v1/streaming`, 'user')
}

public async publicStreaming(): Promise<WebSocket> {
const url = this.streamingURL()
const url = await this.streamingURL()
return this.client.socket(`${url}/api/v1/streaming`, 'public')
}

public async localStreaming(): Promise<WebSocket> {
const url = this.streamingURL()
const url = await this.streamingURL()
return this.client.socket(`${url}/api/v1/streaming`, 'public:local')
}

public async tagStreaming(tag: string): Promise<WebSocket> {
const url = this.streamingURL()
const url = await this.streamingURL()
return this.client.socket(`${url}/api/v1/streaming`, 'hashtag', `tag=${tag}`)
}

public async listStreaming(list_id: string): Promise<WebSocket> {
const url = this.streamingURL()
const url = await this.streamingURL()
return this.client.socket(`${url}/api/v1/streaming`, 'list', `list=${list_id}`)
}

public async directStreaming(): Promise<WebSocket> {
const url = this.streamingURL()
const url = await this.streamingURL()
return this.client.socket(`${url}/api/v1/streaming`, 'direct')
}
}

0 comments on commit a91a460

Please sign in to comment.